2 * Copyright (c) 1997 - 2007 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "krb5_locl.h"
36 RCSID("$Id: init_creds_pw.c 23316 2008-06-23 04:32:32Z lha $");
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_message(context, ENOMEM, "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_message(context, ENOMEM, "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_set_password (context,
453 asprintf (&p, "%s: %.*s\n",
454 result_code ? "Error" : "Success",
455 (int)result_string.length,
456 result_string.length > 0 ? (char*)result_string.data : "");
458 ret = (*prompter) (context, data, NULL, p, 0, NULL);
460 if (result_code == 0) {
461 strlcpy (newpw, buf1, newpw_sz);
465 krb5_set_error_message(context, ret, "failed changing password");
469 memset (buf1, 0, sizeof(buf1));
470 memset (buf2, 0, sizeof(buf2));
471 krb5_data_free (&result_string);
472 krb5_data_free (&result_code_string);
473 krb5_free_cred_contents (context, &cpw_cred);
477 krb5_error_code KRB5_LIB_FUNCTION
478 krb5_keyblock_key_proc (krb5_context context,
481 krb5_const_pointer keyseed,
484 return krb5_copy_keyblock (context, keyseed, key);
487 krb5_error_code KRB5_LIB_FUNCTION
488 krb5_get_init_creds_keytab(krb5_context context,
490 krb5_principal client,
492 krb5_deltat start_time,
493 const char *in_tkt_service,
494 krb5_get_init_creds_opt *options)
496 krb5_get_init_creds_ctx ctx;
498 krb5_keytab_key_proc_args *a;
500 ret = get_init_creds_common(context, client, start_time,
501 in_tkt_service, options, &ctx);
505 a = malloc (sizeof(*a));
508 krb5_set_error_message(context, ret, "malloc: out of memory");
511 a->principal = ctx.cred.client;
514 ret = krb5_get_in_cred (context,
515 KDCOptions2int(ctx.flags),
520 krb5_keytab_key_proc,
528 if (ret == 0 && creds)
531 krb5_free_cred_contents (context, &ctx.cred);
534 free_init_creds_ctx(context, &ctx);
542 static krb5_error_code
543 init_creds_init_as_req (krb5_context context,
545 const krb5_creds *creds,
546 const krb5_addresses *addrs,
547 const krb5_enctype *etypes,
552 memset(a, 0, sizeof(*a));
555 a->msg_type = krb_as_req;
556 a->req_body.kdc_options = opts;
557 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
558 if (a->req_body.cname == NULL) {
560 krb5_set_error_message(context, ret, "malloc: out of memory");
563 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
564 if (a->req_body.sname == NULL) {
566 krb5_set_error_message(context, ret, "malloc: out of memory");
570 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
573 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
577 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
581 if(creds->times.starttime) {
582 a->req_body.from = malloc(sizeof(*a->req_body.from));
583 if (a->req_body.from == NULL) {
585 krb5_set_error_message(context, ret, "malloc: out of memory");
588 *a->req_body.from = creds->times.starttime;
590 if(creds->times.endtime){
591 ALLOC(a->req_body.till, 1);
592 *a->req_body.till = creds->times.endtime;
594 if(creds->times.renew_till){
595 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
596 if (a->req_body.rtime == NULL) {
598 krb5_set_error_message(context, ret, "malloc: out of memory");
601 *a->req_body.rtime = creds->times.renew_till;
603 a->req_body.nonce = 0;
604 ret = krb5_init_etype (context,
605 &a->req_body.etype.len,
606 &a->req_body.etype.val,
612 * This means no addresses
615 if (addrs && addrs->len == 0) {
616 a->req_body.addresses = NULL;
618 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
619 if (a->req_body.addresses == NULL) {
621 krb5_set_error_message(context, ret, "malloc: out of memory");
626 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
628 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
629 if(ret == 0 && a->req_body.addresses->len == 0) {
630 free(a->req_body.addresses);
631 a->req_body.addresses = NULL;
638 a->req_body.enc_authorization_data = NULL;
639 a->req_body.additional_tickets = NULL;
646 memset(a, 0, sizeof(*a));
650 struct pa_info_data {
653 krb5_data *s2kparams;
657 free_paid(krb5_context context, struct pa_info_data *ppaid)
659 krb5_free_salt(context, ppaid->salt);
660 if (ppaid->s2kparams)
661 krb5_free_data(context, ppaid->s2kparams);
665 static krb5_error_code
666 set_paid(struct pa_info_data *paid, krb5_context context,
668 krb5_salttype salttype, void *salt_string, size_t salt_len,
669 krb5_data *s2kparams)
672 paid->salt.salttype = salttype;
673 paid->salt.saltvalue.data = malloc(salt_len + 1);
674 if (paid->salt.saltvalue.data == NULL) {
675 krb5_clear_error_string(context);
678 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
679 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
680 paid->salt.saltvalue.length = salt_len;
684 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
686 krb5_clear_error_string(context);
687 krb5_free_salt(context, paid->salt);
691 paid->s2kparams = NULL;
696 static struct pa_info_data *
697 pa_etype_info2(krb5_context context,
698 const krb5_principal client,
700 struct pa_info_data *paid,
701 heim_octet_string *data)
708 memset(&e, 0, sizeof(e));
709 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
714 for (j = 0; j < asreq->req_body.etype.len; j++) {
715 for (i = 0; i < e.len; i++) {
716 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
718 if (e.val[i].salt == NULL)
719 ret = krb5_get_pw_salt(context, client, &salt);
721 salt.saltvalue.data = *e.val[i].salt;
722 salt.saltvalue.length = strlen(*e.val[i].salt);
726 ret = set_paid(paid, context, e.val[i].etype,
729 salt.saltvalue.length,
731 if (e.val[i].salt == NULL)
732 krb5_free_salt(context, salt);
734 free_ETYPE_INFO2(&e);
741 free_ETYPE_INFO2(&e);
745 static struct pa_info_data *
746 pa_etype_info(krb5_context context,
747 const krb5_principal client,
749 struct pa_info_data *paid,
750 heim_octet_string *data)
757 memset(&e, 0, sizeof(e));
758 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
763 for (j = 0; j < asreq->req_body.etype.len; j++) {
764 for (i = 0; i < e.len; i++) {
765 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
767 salt.salttype = KRB5_PW_SALT;
768 if (e.val[i].salt == NULL)
769 ret = krb5_get_pw_salt(context, client, &salt);
771 salt.saltvalue = *e.val[i].salt;
774 if (e.val[i].salttype)
775 salt.salttype = *e.val[i].salttype;
777 ret = set_paid(paid, context, e.val[i].etype,
780 salt.saltvalue.length,
782 if (e.val[i].salt == NULL)
783 krb5_free_salt(context, salt);
797 static struct pa_info_data *
798 pa_pw_or_afs3_salt(krb5_context context,
799 const krb5_principal client,
801 struct pa_info_data *paid,
802 heim_octet_string *data)
805 if (paid->etype == ENCTYPE_NULL)
807 ret = set_paid(paid, context,
820 krb5_preauthtype type;
821 struct pa_info_data *(*salt_info)(krb5_context,
822 const krb5_principal,
824 struct pa_info_data *,
825 heim_octet_string *);
828 static struct pa_info pa_prefs[] = {
829 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
830 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
831 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
832 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
836 find_pa_data(const METHOD_DATA *md, int type)
841 for (i = 0; i < md->len; i++)
842 if (md->val[i].padata_type == type)
847 static struct pa_info_data *
848 process_pa_info(krb5_context context,
849 const krb5_principal client,
851 struct pa_info_data *paid,
854 struct pa_info_data *p = NULL;
857 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
858 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
861 paid->salt.salttype = pa_prefs[i].type;
862 p = (*pa_prefs[i].salt_info)(context, client, asreq,
863 paid, &pa->padata_value);
868 static krb5_error_code
869 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
870 krb5_enctype etype, krb5_keyblock *key)
876 EncryptedData encdata;
882 krb5_us_timeofday (context, &p.patimestamp, &usec);
886 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
890 krb5_abortx(context, "internal error in ASN.1 encoder");
892 ret = krb5_crypto_init(context, key, 0, &crypto);
897 ret = krb5_encrypt_EncryptedData(context,
899 KRB5_KU_PA_ENC_TIMESTAMP,
905 krb5_crypto_destroy(context, crypto);
909 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
910 free_EncryptedData(&encdata);
914 krb5_abortx(context, "internal error in ASN.1 encoder");
916 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
922 static krb5_error_code
923 add_enc_ts_padata(krb5_context context,
925 krb5_principal client,
926 krb5_s2k_proc key_proc,
927 krb5_const_pointer keyseed,
928 krb5_enctype *enctypes,
931 krb5_data *s2kparams)
939 /* default to standard salt */
940 ret = krb5_get_pw_salt (context, client, &salt2);
944 enctypes = context->etypes;
946 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
950 for (i = 0; i < netypes; ++i) {
953 ret = (*key_proc)(context, enctypes[i], keyseed,
954 *salt, s2kparams, &key);
957 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
958 krb5_free_keyblock (context, key);
963 krb5_free_salt(context, salt2);
967 static krb5_error_code
968 pa_data_to_md_ts_enc(krb5_context context,
970 const krb5_principal client,
971 krb5_get_init_creds_ctx *ctx,
972 struct pa_info_data *ppaid,
975 if (ctx->key_proc == NULL || ctx->password == NULL)
979 add_enc_ts_padata(context, md, client,
980 ctx->key_proc, ctx->password,
982 &ppaid->salt, ppaid->s2kparams);
986 /* make a v5 salted pa-data */
987 add_enc_ts_padata(context, md, client,
988 ctx->key_proc, ctx->password,
989 a->req_body.etype.val, a->req_body.etype.len,
992 /* make a v4 salted pa-data */
993 salt.salttype = KRB5_PW_SALT;
994 krb5_data_zero(&salt.saltvalue);
995 add_enc_ts_padata(context, md, client,
996 ctx->key_proc, ctx->password,
997 a->req_body.etype.val, a->req_body.etype.len,
1003 static krb5_error_code
1004 pa_data_to_key_plain(krb5_context context,
1005 const krb5_principal client,
1006 krb5_get_init_creds_ctx *ctx,
1008 krb5_data *s2kparams,
1010 krb5_keyblock **key)
1012 krb5_error_code ret;
1014 ret = (*ctx->key_proc)(context, etype, ctx->password,
1015 salt, s2kparams, key);
1020 static krb5_error_code
1021 pa_data_to_md_pkinit(krb5_context context,
1023 const krb5_principal client,
1024 krb5_get_init_creds_ctx *ctx,
1027 if (ctx->pk_init_ctx == NULL)
1030 return _krb5_pk_mk_padata(context,
1036 krb5_set_error_message(context, EINVAL, "no support for PKINIT compiled in");
1041 static krb5_error_code
1042 pa_data_add_pac_request(krb5_context context,
1043 krb5_get_init_creds_ctx *ctx,
1047 krb5_error_code ret;
1051 switch (ctx->req_pac) {
1052 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1053 return 0; /* don't bother */
1054 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1055 req.include_pac = 1;
1057 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1058 req.include_pac = 0;
1061 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1066 krb5_abortx(context, "internal error in ASN.1 encoder");
1068 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1076 * Assumes caller always will free `out_md', even on error.
1079 static krb5_error_code
1080 process_pa_data_to_md(krb5_context context,
1081 const krb5_creds *creds,
1083 krb5_get_init_creds_ctx *ctx,
1085 METHOD_DATA **out_md,
1086 krb5_prompter_fct prompter,
1087 void *prompter_data)
1089 krb5_error_code ret;
1092 if (*out_md == NULL) {
1093 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
1097 (*out_md)->val = NULL;
1100 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1101 * need to expose our password protecting our PKCS12 key.
1104 if (ctx->pk_init_ctx) {
1106 ret = pa_data_to_md_pkinit(context, a, creds->client, ctx, *out_md);
1110 } else if (in_md->len != 0) {
1111 struct pa_info_data paid, *ppaid;
1113 memset(&paid, 0, sizeof(paid));
1115 paid.etype = ENCTYPE_NULL;
1116 ppaid = process_pa_info(context, creds->client, a, &paid, in_md);
1118 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1120 free_paid(context, ppaid);
1123 pa_data_add_pac_request(context, ctx, *out_md);
1125 if ((*out_md)->len == 0) {
1133 static krb5_error_code
1134 process_pa_data_to_key(krb5_context context,
1135 krb5_get_init_creds_ctx *ctx,
1139 const krb5_krbhst_info *hi,
1140 krb5_keyblock **key)
1142 struct pa_info_data paid, *ppaid = NULL;
1143 krb5_error_code ret;
1147 memset(&paid, 0, sizeof(paid));
1149 etype = rep->kdc_rep.enc_part.etype;
1151 if (rep->kdc_rep.padata) {
1153 ppaid = process_pa_info(context, creds->client, a, &paid,
1154 rep->kdc_rep.padata);
1156 if (ppaid == NULL) {
1157 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1161 paid.s2kparams = NULL;
1165 if (rep->kdc_rep.padata) {
1167 pa = krb5_find_padata(rep->kdc_rep.padata->val,
1168 rep->kdc_rep.padata->len,
1169 KRB5_PADATA_PK_AS_REP,
1173 pa = krb5_find_padata(rep->kdc_rep.padata->val,
1174 rep->kdc_rep.padata->len,
1175 KRB5_PADATA_PK_AS_REP_19,
1179 if (pa && ctx->pk_init_ctx) {
1181 ret = _krb5_pk_rd_pa_reply(context,
1192 krb5_set_error_message(context, ret, "no support for PKINIT compiled in");
1194 } else if (ctx->password)
1195 ret = pa_data_to_key_plain(context, creds->client, ctx,
1196 paid.salt, paid.s2kparams, etype, key);
1199 krb5_set_error_message(context, ret, "No usable pa data type");
1202 free_paid(context, &paid);
1206 static krb5_error_code
1207 init_cred_loop(krb5_context context,
1208 krb5_get_init_creds_opt *init_cred_opts,
1209 const krb5_prompter_fct prompter,
1210 void *prompter_data,
1211 krb5_get_init_creds_ctx *ctx,
1213 krb5_kdc_rep *ret_as_reply)
1215 krb5_error_code ret;
1221 krb5_krbhst_info *hi = NULL;
1222 krb5_sendto_ctx stctx = NULL;
1225 memset(&md, 0, sizeof(md));
1226 memset(&rep, 0, sizeof(rep));
1228 _krb5_get_init_creds_opt_free_krb5_error(init_cred_opts);
1231 memset(ret_as_reply, 0, sizeof(*ret_as_reply));
1233 ret = init_creds_init_as_req(context, ctx->flags, creds,
1234 ctx->addrs, ctx->etypes, &ctx->as_req);
1238 ret = krb5_sendto_ctx_alloc(context, &stctx);
1241 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1243 /* Set a new nonce. */
1244 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1245 ctx->nonce &= 0xffffffff;
1246 /* XXX these just needs to be the same when using Windows PK-INIT */
1247 ctx->pk_nonce = ctx->nonce;
1250 * Increase counter when we want other pre-auth types then
1251 * KRB5_PA_ENC_TIMESTAMP.
1253 #define MAX_PA_COUNTER 3
1255 ctx->pa_counter = 0;
1256 while (ctx->pa_counter < MAX_PA_COUNTER) {
1260 if (ctx->as_req.padata) {
1261 free_METHOD_DATA(ctx->as_req.padata);
1262 free(ctx->as_req.padata);
1263 ctx->as_req.padata = NULL;
1266 /* Set a new nonce. */
1267 ctx->as_req.req_body.nonce = ctx->nonce;
1269 /* fill_in_md_data */
1270 ret = process_pa_data_to_md(context, creds, &ctx->as_req, ctx,
1271 &md, &ctx->as_req.padata,
1272 prompter, prompter_data);
1276 krb5_data_free(&ctx->req_buffer);
1278 ASN1_MALLOC_ENCODE(AS_REQ,
1279 ctx->req_buffer.data, ctx->req_buffer.length,
1280 &ctx->as_req, &len, ret);
1283 if(len != ctx->req_buffer.length)
1284 krb5_abortx(context, "internal error in ASN.1 encoder");
1286 ret = krb5_sendto_context (context, stctx, &ctx->req_buffer,
1287 creds->client->realm, &resp);
1291 memset (&rep, 0, sizeof(rep));
1292 ret = decode_AS_REP(resp.data, resp.length, &rep.kdc_rep, &size);
1294 krb5_data_free(&resp);
1295 krb5_clear_error_string(context);
1298 /* let's try to parse it as a KRB-ERROR */
1301 ret = krb5_rd_error(context, &resp, &error);
1302 if(ret && resp.data && ((char*)resp.data)[0] == 4)
1303 ret = KRB5KRB_AP_ERR_V4_REPLY;
1304 krb5_data_free(&resp);
1308 ret = krb5_error_from_rd_error(context, &error, creds);
1311 * If no preauth was set and KDC requires it, give it one
1315 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1316 free_METHOD_DATA(&md);
1317 memset(&md, 0, sizeof(md));
1320 ret = decode_METHOD_DATA(error.e_data->data,
1321 error.e_data->length,
1325 krb5_set_error_message(context, ret,
1326 "failed to decode METHOD DATA");
1328 /* XXX guess what the server want here add add md */
1330 krb5_free_error_contents(context, &error);
1334 _krb5_get_init_creds_opt_set_krb5_error(context,
1340 krb5_free_error_contents(context, &error);
1347 krb5_keyblock *key = NULL;
1348 unsigned flags = EXTRACT_TICKET_AS_REQ;
1350 if (ctx->flags.request_anonymous)
1351 flags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1352 if (ctx->flags.canonicalize) {
1353 flags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1354 flags |= EXTRACT_TICKET_MATCH_REALM;
1356 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1357 flags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1359 ret = process_pa_data_to_key(context, ctx, creds,
1360 &ctx->as_req, &rep, hi, &key);
1364 ret = _krb5_extract_ticket(context,
1369 KRB5_KU_AS_REP_ENC_PART,
1375 krb5_free_keyblock(context, key);
1379 krb5_sendto_ctx_free(context, stctx);
1380 krb5_data_free(&ctx->req_buffer);
1381 free_METHOD_DATA(&md);
1382 memset(&md, 0, sizeof(md));
1384 if (ret == 0 && ret_as_reply)
1385 *ret_as_reply = rep;
1387 krb5_free_kdc_rep (context, &rep);
1391 krb5_error_code KRB5_LIB_FUNCTION
1392 krb5_get_init_creds(krb5_context context,
1394 krb5_principal client,
1395 krb5_prompter_fct prompter,
1397 krb5_deltat start_time,
1398 const char *in_tkt_service,
1399 krb5_get_init_creds_opt *options)
1401 krb5_get_init_creds_ctx ctx;
1402 krb5_kdc_rep kdc_reply;
1403 krb5_error_code ret;
1407 memset(&kdc_reply, 0, sizeof(kdc_reply));
1409 ret = get_init_creds_common(context, client, start_time,
1410 in_tkt_service, options, &ctx);
1416 memset(&kdc_reply, 0, sizeof(kdc_reply));
1418 ret = init_cred_loop(context,
1430 case KRB5KDC_ERR_KEY_EXPIRED :
1431 /* try to avoid recursion */
1433 /* don't try to change password where then where none */
1434 if (prompter == NULL || ctx.password == NULL)
1437 krb5_clear_error_string (context);
1439 if (ctx.in_tkt_service != NULL
1440 && strcmp (ctx.in_tkt_service, "kadmin/changepw") == 0)
1443 ret = change_password (context,
1461 print_expire (context,
1462 krb5_principal_get_realm (context, ctx.cred.client),
1468 memset (buf, 0, sizeof(buf));
1469 free_init_creds_ctx(context, &ctx);
1470 krb5_free_kdc_rep (context, &kdc_reply);
1474 krb5_free_cred_contents (context, &ctx.cred);
1479 krb5_error_code KRB5_LIB_FUNCTION
1480 krb5_get_init_creds_password(krb5_context context,
1482 krb5_principal client,
1483 const char *password,
1484 krb5_prompter_fct prompter,
1486 krb5_deltat start_time,
1487 const char *in_tkt_service,
1488 krb5_get_init_creds_opt *in_options)
1490 krb5_get_init_creds_opt *options;
1492 krb5_error_code ret;
1494 if (in_options == NULL) {
1495 const char *realm = krb5_principal_get_realm(context, client);
1496 ret = krb5_get_init_creds_opt_alloc(context, &options);
1498 krb5_get_init_creds_opt_set_default_flags(context,
1503 ret = _krb5_get_init_creds_opt_copy(context, in_options, &options);
1507 if (password == NULL &&
1508 options->opt_private->password == NULL &&
1509 options->opt_private->pk_init_ctx == NULL)
1512 krb5_data password_data;
1515 krb5_unparse_name (context, client, &p);
1516 asprintf (&q, "%s's Password: ", p);
1519 password_data.data = buf;
1520 password_data.length = sizeof(buf);
1522 prompt.reply = &password_data;
1523 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1525 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
1528 memset (buf, 0, sizeof(buf));
1529 krb5_get_init_creds_opt_free(context, options);
1530 ret = KRB5_LIBOS_PWDINTR;
1531 krb5_clear_error_string (context);
1534 password = password_data.data;
1537 if (options->opt_private->password == NULL) {
1538 ret = krb5_get_init_creds_opt_set_pa_password(context, options,
1541 krb5_get_init_creds_opt_free(context, options);
1542 memset(buf, 0, sizeof(buf));
1547 ret = krb5_get_init_creds(context, creds, client, prompter,
1548 data, start_time, in_tkt_service, options);
1549 krb5_get_init_creds_opt_free(context, options);
1550 memset(buf, 0, sizeof(buf));
1554 static krb5_error_code
1555 init_creds_keyblock_key_proc (krb5_context context,
1558 krb5_const_pointer keyseed,
1559 krb5_keyblock **key)
1561 return krb5_copy_keyblock (context, keyseed, key);
1564 krb5_error_code KRB5_LIB_FUNCTION
1565 krb5_get_init_creds_keyblock(krb5_context context,
1567 krb5_principal client,
1568 krb5_keyblock *keyblock,
1569 krb5_deltat start_time,
1570 const char *in_tkt_service,
1571 krb5_get_init_creds_opt *options)
1573 struct krb5_get_init_creds_ctx ctx;
1574 krb5_error_code ret;
1576 ret = get_init_creds_common(context, client, start_time,
1577 in_tkt_service, options, &ctx);
1581 ret = krb5_get_in_cred (context,
1582 KDCOptions2int(ctx.flags),
1587 init_creds_keyblock_key_proc,
1594 if (ret == 0 && creds)
1597 krb5_free_cred_contents (context, &ctx.cred);
1600 free_init_creds_ctx(context, &ctx);