2 * Copyright (c) 1997 - 2008 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 typedef struct krb5_get_init_creds_ctx {
39 krb5_addresses *addrs;
41 krb5_preauthtype *pre_auth_types;
50 /* password and keytab_data is freed on completion */
52 krb5_keytab_key_proc_args *keytab_data;
54 krb5_pointer *keyseed;
55 krb5_s2k_proc keyproc;
57 krb5_get_init_creds_tristate req_pac;
59 krb5_pk_init_ctx pk_init_ctx;
65 EncKDCRepPart enc_part;
67 krb5_prompter_fct prompter;
70 } krb5_get_init_creds_ctx;
72 static krb5_error_code
73 default_s2k_func(krb5_context context, krb5_enctype type,
74 krb5_const_pointer keyseed,
75 krb5_salt salt, krb5_data *s2kparms,
82 password.data = rk_UNCONST(keyseed);
83 password.length = strlen(keyseed);
87 krb5_data_zero(&opaque);
89 *key = malloc(sizeof(**key));
92 ret = krb5_string_to_key_data_salt_opaque(context, type, password,
102 free_init_creds_ctx(krb5_context context, krb5_init_creds_context ctx)
106 if (ctx->pre_auth_types)
107 free (ctx->pre_auth_types);
108 if (ctx->in_tkt_service)
109 free(ctx->in_tkt_service);
110 if (ctx->keytab_data)
111 free(ctx->keytab_data);
112 krb5_data_free(&ctx->req_buffer);
113 krb5_free_cred_contents(context, &ctx->cred);
114 free_METHOD_DATA(&ctx->md);
115 free_AS_REP(&ctx->as_rep);
116 free_EncKDCRepPart(&ctx->enc_part);
117 free_KRB_ERROR(&ctx->error);
118 free_AS_REQ(&ctx->as_req);
119 memset(ctx, 0, sizeof(*ctx));
123 get_config_time (krb5_context context,
130 ret = krb5_config_get_time (context, NULL,
137 ret = krb5_config_get_time (context, NULL,
146 static krb5_error_code
147 init_cred (krb5_context context,
149 krb5_principal client,
150 krb5_deltat start_time,
151 krb5_get_init_creds_opt *options)
157 krb5_timeofday (context, &now);
159 memset (cred, 0, sizeof(*cred));
162 krb5_copy_principal(context, client, &cred->client);
164 ret = krb5_get_default_principal (context,
171 cred->times.starttime = now + start_time;
173 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
174 tmp = options->tkt_life;
177 cred->times.endtime = now + tmp;
179 if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE) &&
180 options->renew_life > 0) {
181 cred->times.renew_till = now + options->renew_life;
187 krb5_free_cred_contents (context, cred);
192 * Print a message (str) to the user about the expiration in `lr'
196 report_expiration (krb5_context context,
197 krb5_prompter_fct prompter,
204 asprintf (&p, "%s%s", str, ctime(&now));
205 (*prompter) (context, data, NULL, p, 0, NULL);
210 * Check the context, and in the case there is a expiration warning,
211 * use the prompter to print the warning.
213 * @param context A Kerberos 5 context.
214 * @param options An GIC options structure
215 * @param ctx The krb5_init_creds_context check for expiration.
218 static krb5_error_code
219 process_last_request(krb5_context context,
220 krb5_get_init_creds_opt *options,
221 krb5_init_creds_context ctx)
223 krb5_const_realm realm;
225 krb5_boolean reported = FALSE;
231 * First check if there is a API consumer.
234 realm = krb5_principal_get_realm (context, ctx->cred.client);
235 lr = &ctx->enc_part.last_req;
237 if (options && options->opt_private && options->opt_private->lr.func) {
238 krb5_last_req_entry **lre;
240 lre = calloc(lr->len + 1, sizeof(**lre));
242 krb5_set_error_message(context, ENOMEM,
243 N_("malloc: out of memory", ""));
246 for (i = 0; i < lr->len; i++) {
247 lre[i] = calloc(1, sizeof(*lre[i]));
250 lre[i]->lr_type = lr->val[i].lr_type;
251 lre[i]->value = lr->val[i].lr_value;
254 (*options->opt_private->lr.func)(context, lre,
255 options->opt_private->lr.ctx);
257 for (i = 0; i < lr->len; i++)
263 * Now check if we should prompt the user
266 if (ctx->prompter == NULL)
269 krb5_timeofday (context, &sec);
271 t = sec + get_config_time (context,
276 for (i = 0; i < lr->len; ++i) {
277 if (lr->val[i].lr_value <= t) {
278 switch (abs(lr->val[i].lr_type)) {
280 report_expiration(context, ctx->prompter,
282 "Your password will expire at ",
283 lr->val[i].lr_value);
286 case LR_ACCT_EXPTIME :
287 report_expiration(context, ctx->prompter,
289 "Your account will expire at ",
290 lr->val[i].lr_value);
298 && ctx->enc_part.key_expiration
299 && *ctx->enc_part.key_expiration <= t) {
300 report_expiration(context, ctx->prompter,
302 "Your password/account will expire at ",
303 *ctx->enc_part.key_expiration);
308 static krb5_addresses no_addrs = { 0, NULL };
310 static krb5_error_code
311 get_init_creds_common(krb5_context context,
312 krb5_principal client,
313 krb5_deltat start_time,
314 krb5_get_init_creds_opt *options,
315 krb5_init_creds_context ctx)
317 krb5_get_init_creds_opt *default_opt = NULL;
319 krb5_enctype *etypes;
320 krb5_preauthtype *pre_auth_types;
322 memset(ctx, 0, sizeof(*ctx));
324 if (options == NULL) {
325 const char *realm = krb5_principal_get_realm(context, client);
327 krb5_get_init_creds_opt_alloc (context, &default_opt);
328 options = default_opt;
329 krb5_get_init_creds_opt_set_default_flags(context, NULL, realm, options);
332 if (options->opt_private) {
333 if (options->opt_private->password) {
334 ret = krb5_init_creds_set_password(context, ctx,
335 options->opt_private->password);
340 ctx->keyproc = options->opt_private->key_proc;
341 ctx->req_pac = options->opt_private->req_pac;
342 ctx->pk_init_ctx = options->opt_private->pk_init_ctx;
343 ctx->ic_flags = options->opt_private->flags;
345 ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET;
347 if (ctx->keyproc == NULL)
348 ctx->keyproc = default_s2k_func;
350 /* Enterprise name implicitly turns on canonicalize */
351 if ((ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE) ||
352 krb5_principal_get_type(context, client) == KRB5_NT_ENTERPRISE_PRINCIPAL)
353 ctx->flags.canonicalize = 1;
355 ctx->pre_auth_types = NULL;
358 ctx->pre_auth_types = NULL;
360 ret = init_cred(context, &ctx->cred, client, start_time, options);
363 krb5_get_init_creds_opt_free(context, default_opt);
367 ret = krb5_init_creds_set_service(context, ctx, NULL);
371 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
372 ctx->flags.forwardable = options->forwardable;
374 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
375 ctx->flags.proxiable = options->proxiable;
378 ctx->flags.postdated = 1;
379 if (ctx->cred.times.renew_till)
380 ctx->flags.renewable = 1;
381 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) {
382 ctx->addrs = options->address_list;
383 } else if (options->opt_private) {
384 switch (options->opt_private->addressless) {
385 case KRB5_INIT_CREDS_TRISTATE_UNSET:
386 #if KRB5_ADDRESSLESS_DEFAULT == TRUE
387 ctx->addrs = &no_addrs;
392 case KRB5_INIT_CREDS_TRISTATE_FALSE:
395 case KRB5_INIT_CREDS_TRISTATE_TRUE:
396 ctx->addrs = &no_addrs;
400 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
401 etypes = malloc((options->etype_list_length + 1)
402 * sizeof(krb5_enctype));
403 if (etypes == NULL) {
405 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
408 memcpy (etypes, options->etype_list,
409 options->etype_list_length * sizeof(krb5_enctype));
410 etypes[options->etype_list_length] = ETYPE_NULL;
411 ctx->etypes = etypes;
413 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
414 pre_auth_types = malloc((options->preauth_list_length + 1)
415 * sizeof(krb5_preauthtype));
416 if (pre_auth_types == NULL) {
418 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
421 memcpy (pre_auth_types, options->preauth_list,
422 options->preauth_list_length * sizeof(krb5_preauthtype));
423 pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE;
424 ctx->pre_auth_types = pre_auth_types;
426 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
427 ctx->flags.request_anonymous = options->anonymous;
429 krb5_get_init_creds_opt_free(context, default_opt);
433 krb5_get_init_creds_opt_free(context, default_opt);
437 static krb5_error_code
438 change_password (krb5_context context,
439 krb5_principal client,
440 const char *password,
443 krb5_prompter_fct prompter,
445 krb5_get_init_creds_opt *old_options)
447 krb5_prompt prompts[2];
450 char buf1[BUFSIZ], buf2[BUFSIZ];
451 krb5_data password_data[2];
453 krb5_data result_code_string;
454 krb5_data result_string;
456 krb5_get_init_creds_opt *options;
458 memset (&cpw_cred, 0, sizeof(cpw_cred));
460 ret = krb5_get_init_creds_opt_alloc(context, &options);
463 krb5_get_init_creds_opt_set_tkt_life (options, 60);
464 krb5_get_init_creds_opt_set_forwardable (options, FALSE);
465 krb5_get_init_creds_opt_set_proxiable (options, FALSE);
466 if (old_options && old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
467 krb5_get_init_creds_opt_set_preauth_list (options,
468 old_options->preauth_list,
469 old_options->preauth_list_length);
471 krb5_data_zero (&result_code_string);
472 krb5_data_zero (&result_string);
474 ret = krb5_get_init_creds_password (context,
483 krb5_get_init_creds_opt_free(context, options);
488 password_data[0].data = buf1;
489 password_data[0].length = sizeof(buf1);
491 prompts[0].hidden = 1;
492 prompts[0].prompt = "New password: ";
493 prompts[0].reply = &password_data[0];
494 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
496 password_data[1].data = buf2;
497 password_data[1].length = sizeof(buf2);
499 prompts[1].hidden = 1;
500 prompts[1].prompt = "Repeat new password: ";
501 prompts[1].reply = &password_data[1];
502 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
504 ret = (*prompter) (context, data, NULL, "Changing password",
507 memset (buf1, 0, sizeof(buf1));
508 memset (buf2, 0, sizeof(buf2));
512 if (strcmp (buf1, buf2) == 0)
514 memset (buf1, 0, sizeof(buf1));
515 memset (buf2, 0, sizeof(buf2));
518 ret = krb5_set_password (context,
527 asprintf (&p, "%s: %.*s\n",
528 result_code ? "Error" : "Success",
529 (int)result_string.length,
530 result_string.length > 0 ? (char*)result_string.data : "");
532 /* return the result */
533 (*prompter) (context, data, NULL, p, 0, NULL);
536 if (result_code == 0) {
537 strlcpy (newpw, buf1, newpw_sz);
541 krb5_set_error_message(context, ret,
542 N_("failed changing password", ""));
546 memset (buf1, 0, sizeof(buf1));
547 memset (buf2, 0, sizeof(buf2));
548 krb5_data_free (&result_string);
549 krb5_data_free (&result_code_string);
550 krb5_free_cred_contents (context, &cpw_cred);
555 krb5_error_code KRB5_LIB_FUNCTION
556 krb5_keyblock_key_proc (krb5_context context,
559 krb5_const_pointer keyseed,
562 return krb5_copy_keyblock (context, keyseed, key);
569 static krb5_error_code
570 init_as_req (krb5_context context,
572 const krb5_creds *creds,
573 const krb5_addresses *addrs,
574 const krb5_enctype *etypes,
579 memset(a, 0, sizeof(*a));
582 a->msg_type = krb_as_req;
583 a->req_body.kdc_options = opts;
584 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
585 if (a->req_body.cname == NULL) {
587 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
590 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
591 if (a->req_body.sname == NULL) {
593 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
597 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
600 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
604 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
608 if(creds->times.starttime) {
609 a->req_body.from = malloc(sizeof(*a->req_body.from));
610 if (a->req_body.from == NULL) {
612 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
615 *a->req_body.from = creds->times.starttime;
617 if(creds->times.endtime){
618 ALLOC(a->req_body.till, 1);
619 *a->req_body.till = creds->times.endtime;
621 if(creds->times.renew_till){
622 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
623 if (a->req_body.rtime == NULL) {
625 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
628 *a->req_body.rtime = creds->times.renew_till;
630 a->req_body.nonce = 0;
631 ret = krb5_init_etype (context,
632 &a->req_body.etype.len,
633 &a->req_body.etype.val,
639 * This means no addresses
642 if (addrs && addrs->len == 0) {
643 a->req_body.addresses = NULL;
645 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
646 if (a->req_body.addresses == NULL) {
648 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
653 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
655 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
656 if(ret == 0 && a->req_body.addresses->len == 0) {
657 free(a->req_body.addresses);
658 a->req_body.addresses = NULL;
665 a->req_body.enc_authorization_data = NULL;
666 a->req_body.additional_tickets = NULL;
673 memset(a, 0, sizeof(*a));
677 struct pa_info_data {
680 krb5_data *s2kparams;
684 free_paid(krb5_context context, struct pa_info_data *ppaid)
686 krb5_free_salt(context, ppaid->salt);
687 if (ppaid->s2kparams)
688 krb5_free_data(context, ppaid->s2kparams);
692 static krb5_error_code
693 set_paid(struct pa_info_data *paid, krb5_context context,
695 krb5_salttype salttype, void *salt_string, size_t salt_len,
696 krb5_data *s2kparams)
699 paid->salt.salttype = salttype;
700 paid->salt.saltvalue.data = malloc(salt_len + 1);
701 if (paid->salt.saltvalue.data == NULL) {
702 krb5_clear_error_message(context);
705 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
706 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
707 paid->salt.saltvalue.length = salt_len;
711 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
713 krb5_clear_error_message(context);
714 krb5_free_salt(context, paid->salt);
718 paid->s2kparams = NULL;
723 static struct pa_info_data *
724 pa_etype_info2(krb5_context context,
725 const krb5_principal client,
727 struct pa_info_data *paid,
728 heim_octet_string *data)
735 memset(&e, 0, sizeof(e));
736 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
741 for (j = 0; j < asreq->req_body.etype.len; j++) {
742 for (i = 0; i < e.len; i++) {
743 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
745 if (e.val[i].salt == NULL)
746 ret = krb5_get_pw_salt(context, client, &salt);
748 salt.saltvalue.data = *e.val[i].salt;
749 salt.saltvalue.length = strlen(*e.val[i].salt);
753 ret = set_paid(paid, context, e.val[i].etype,
756 salt.saltvalue.length,
758 if (e.val[i].salt == NULL)
759 krb5_free_salt(context, salt);
761 free_ETYPE_INFO2(&e);
768 free_ETYPE_INFO2(&e);
772 static struct pa_info_data *
773 pa_etype_info(krb5_context context,
774 const krb5_principal client,
776 struct pa_info_data *paid,
777 heim_octet_string *data)
784 memset(&e, 0, sizeof(e));
785 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
790 for (j = 0; j < asreq->req_body.etype.len; j++) {
791 for (i = 0; i < e.len; i++) {
792 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
794 salt.salttype = KRB5_PW_SALT;
795 if (e.val[i].salt == NULL)
796 ret = krb5_get_pw_salt(context, client, &salt);
798 salt.saltvalue = *e.val[i].salt;
801 if (e.val[i].salttype)
802 salt.salttype = *e.val[i].salttype;
804 ret = set_paid(paid, context, e.val[i].etype,
807 salt.saltvalue.length,
809 if (e.val[i].salt == NULL)
810 krb5_free_salt(context, salt);
824 static struct pa_info_data *
825 pa_pw_or_afs3_salt(krb5_context context,
826 const krb5_principal client,
828 struct pa_info_data *paid,
829 heim_octet_string *data)
832 if (paid->etype == ENCTYPE_NULL)
834 ret = set_paid(paid, context,
847 krb5_preauthtype type;
848 struct pa_info_data *(*salt_info)(krb5_context,
849 const krb5_principal,
851 struct pa_info_data *,
852 heim_octet_string *);
855 static struct pa_info pa_prefs[] = {
856 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
857 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
858 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
859 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
863 find_pa_data(const METHOD_DATA *md, int type)
868 for (i = 0; i < md->len; i++)
869 if (md->val[i].padata_type == type)
874 static struct pa_info_data *
875 process_pa_info(krb5_context context,
876 const krb5_principal client,
878 struct pa_info_data *paid,
881 struct pa_info_data *p = NULL;
884 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
885 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
888 paid->salt.salttype = pa_prefs[i].type;
889 p = (*pa_prefs[i].salt_info)(context, client, asreq,
890 paid, &pa->padata_value);
895 static krb5_error_code
896 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
897 krb5_enctype etype, krb5_keyblock *key)
903 EncryptedData encdata;
909 krb5_us_timeofday (context, &p.patimestamp, &usec);
913 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
917 krb5_abortx(context, "internal error in ASN.1 encoder");
919 ret = krb5_crypto_init(context, key, 0, &crypto);
924 ret = krb5_encrypt_EncryptedData(context,
926 KRB5_KU_PA_ENC_TIMESTAMP,
932 krb5_crypto_destroy(context, crypto);
936 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
937 free_EncryptedData(&encdata);
941 krb5_abortx(context, "internal error in ASN.1 encoder");
943 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
949 static krb5_error_code
950 add_enc_ts_padata(krb5_context context,
952 krb5_principal client,
953 krb5_s2k_proc keyproc,
954 krb5_const_pointer keyseed,
955 krb5_enctype *enctypes,
958 krb5_data *s2kparams)
966 /* default to standard salt */
967 ret = krb5_get_pw_salt (context, client, &salt2);
973 enctypes = context->etypes;
975 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
979 for (i = 0; i < netypes; ++i) {
982 ret = (*keyproc)(context, enctypes[i], keyseed,
983 *salt, s2kparams, &key);
986 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
987 krb5_free_keyblock (context, key);
992 krb5_free_salt(context, salt2);
996 static krb5_error_code
997 pa_data_to_md_ts_enc(krb5_context context,
999 const krb5_principal client,
1000 krb5_get_init_creds_ctx *ctx,
1001 struct pa_info_data *ppaid,
1004 if (ctx->keyproc == NULL || ctx->keyseed == NULL)
1008 add_enc_ts_padata(context, md, client,
1009 ctx->keyproc, ctx->keyseed,
1011 &ppaid->salt, ppaid->s2kparams);
1015 /* make a v5 salted pa-data */
1016 add_enc_ts_padata(context, md, client,
1017 ctx->keyproc, ctx->keyseed,
1018 a->req_body.etype.val, a->req_body.etype.len,
1021 /* make a v4 salted pa-data */
1022 salt.salttype = KRB5_PW_SALT;
1023 krb5_data_zero(&salt.saltvalue);
1024 add_enc_ts_padata(context, md, client,
1025 ctx->keyproc, ctx->keyseed,
1026 a->req_body.etype.val, a->req_body.etype.len,
1032 static krb5_error_code
1033 pa_data_to_key_plain(krb5_context context,
1034 const krb5_principal client,
1035 krb5_get_init_creds_ctx *ctx,
1037 krb5_data *s2kparams,
1039 krb5_keyblock **key)
1041 krb5_error_code ret;
1043 ret = (*ctx->keyproc)(context, etype, ctx->keyseed,
1044 salt, s2kparams, key);
1049 static krb5_error_code
1050 pa_data_to_md_pkinit(krb5_context context,
1052 const krb5_principal client,
1053 krb5_get_init_creds_ctx *ctx,
1056 if (ctx->pk_init_ctx == NULL)
1059 return _krb5_pk_mk_padata(context,
1065 krb5_set_error_message(context, EINVAL,
1066 N_("no support for PKINIT compiled in", ""));
1071 static krb5_error_code
1072 pa_data_add_pac_request(krb5_context context,
1073 krb5_get_init_creds_ctx *ctx,
1077 krb5_error_code ret;
1081 switch (ctx->req_pac) {
1082 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1083 return 0; /* don't bother */
1084 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1085 req.include_pac = 1;
1087 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1088 req.include_pac = 0;
1091 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1096 krb5_abortx(context, "internal error in ASN.1 encoder");
1098 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1106 * Assumes caller always will free `out_md', even on error.
1109 static krb5_error_code
1110 process_pa_data_to_md(krb5_context context,
1111 const krb5_creds *creds,
1113 krb5_get_init_creds_ctx *ctx,
1115 METHOD_DATA **out_md,
1116 krb5_prompter_fct prompter,
1117 void *prompter_data)
1119 krb5_error_code ret;
1122 if (*out_md == NULL) {
1123 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1127 (*out_md)->val = NULL;
1130 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1131 * need to expose our password protecting our PKCS12 key.
1134 if (ctx->pk_init_ctx) {
1136 ret = pa_data_to_md_pkinit(context, a, creds->client, ctx, *out_md);
1140 } else if (in_md->len != 0) {
1141 struct pa_info_data paid, *ppaid;
1143 memset(&paid, 0, sizeof(paid));
1145 paid.etype = ENCTYPE_NULL;
1146 ppaid = process_pa_info(context, creds->client, a, &paid, in_md);
1148 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1150 free_paid(context, ppaid);
1153 pa_data_add_pac_request(context, ctx, *out_md);
1155 if ((*out_md)->len == 0) {
1163 static krb5_error_code
1164 process_pa_data_to_key(krb5_context context,
1165 krb5_get_init_creds_ctx *ctx,
1169 const krb5_krbhst_info *hi,
1170 krb5_keyblock **key)
1172 struct pa_info_data paid, *ppaid = NULL;
1173 krb5_error_code ret;
1177 memset(&paid, 0, sizeof(paid));
1179 etype = rep->enc_part.etype;
1183 ppaid = process_pa_info(context, creds->client, a, &paid,
1186 if (ppaid == NULL) {
1187 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1191 paid.s2kparams = NULL;
1197 pa = krb5_find_padata(rep->padata->val,
1199 KRB5_PADATA_PK_AS_REP,
1203 pa = krb5_find_padata(rep->padata->val,
1205 KRB5_PADATA_PK_AS_REP_19,
1209 if (pa && ctx->pk_init_ctx) {
1211 ret = _krb5_pk_rd_pa_reply(context,
1222 krb5_set_error_message(context, ret, N_("no support for PKINIT compiled in", ""));
1224 } else if (ctx->keyseed)
1225 ret = pa_data_to_key_plain(context, creds->client, ctx,
1226 paid.salt, paid.s2kparams, etype, key);
1229 krb5_set_error_message(context, ret, N_("No usable pa data type", ""));
1232 free_paid(context, &paid);
1237 * Start a new context to get a new initial credential.
1239 * @param context A Kerberos 5 context.
1240 * @param client The Kerberos principal to get the credential for, if
1241 * NULL is given, the default principal is used as determined by
1242 * krb5_get_default_principal().
1244 * @param prompter_data
1245 * @param start_time the time the ticket should start to be valid or 0 for now.
1246 * @param options a options structure, can be NULL for default options.
1247 * @param rctx A new allocated free with krb5_init_creds_free().
1249 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
1251 * @ingroup krb5_credential
1254 krb5_error_code KRB5_LIB_FUNCTION
1255 krb5_init_creds_init(krb5_context context,
1256 krb5_principal client,
1257 krb5_prompter_fct prompter,
1258 void *prompter_data,
1259 krb5_deltat start_time,
1260 krb5_get_init_creds_opt *options,
1261 krb5_init_creds_context *rctx)
1263 krb5_init_creds_context ctx;
1264 krb5_error_code ret;
1268 ctx = calloc(1, sizeof(*ctx));
1270 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1274 ret = get_init_creds_common(context, client, start_time, options, ctx);
1280 /* Set a new nonce. */
1281 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1282 ctx->nonce &= 0x7fffffff;
1283 /* XXX these just needs to be the same when using Windows PK-INIT */
1284 ctx->pk_nonce = ctx->nonce;
1286 ctx->prompter = prompter;
1287 ctx->prompter_data = prompter_data;
1295 * Sets the service that the is requested. This call is only neede for
1296 * special initial tickets, by default the a krbtgt is fetched in the default realm.
1298 * @param context a Kerberos 5 context.
1299 * @param ctx a krb5_init_creds_context context.
1300 * @param service the service given as a string, for example
1301 * "kadmind/admin". If NULL, the default krbtgt in the clients
1304 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1305 * @ingroup krb5_credential
1308 krb5_error_code KRB5_LIB_FUNCTION
1309 krb5_init_creds_set_service(krb5_context context,
1310 krb5_init_creds_context ctx,
1311 const char *service)
1313 krb5_const_realm client_realm;
1314 krb5_principal principal;
1315 krb5_error_code ret;
1317 client_realm = krb5_principal_get_realm (context, ctx->cred.client);
1320 ret = krb5_parse_name (context, service, &principal);
1323 krb5_principal_set_realm (context, principal, client_realm);
1325 ret = krb5_make_principal(context, &principal,
1326 client_realm, KRB5_TGS_NAME, client_realm,
1331 krb5_free_principal(context, ctx->cred.server);
1332 ctx->cred.server = principal;
1338 * Sets the password that will use for the request.
1340 * @param context a Kerberos 5 context.
1341 * @param ctx ctx krb5_init_creds_context context.
1342 * @param password the password to use.
1344 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1345 * @ingroup krb5_credential
1348 krb5_error_code KRB5_LIB_FUNCTION
1349 krb5_init_creds_set_password(krb5_context context,
1350 krb5_init_creds_context ctx,
1351 const char *password)
1354 memset(ctx->password, 0, strlen(ctx->password));
1356 ctx->password = strdup(password);
1357 if (ctx->password == NULL) {
1358 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1361 ctx->keyseed = (void *) ctx->password;
1363 ctx->keyseed = NULL;
1364 ctx->password = NULL;
1370 static krb5_error_code
1371 keytab_key_proc(krb5_context context, krb5_enctype enctype,
1372 krb5_const_pointer keyseed,
1373 krb5_salt salt, krb5_data *s2kparms,
1374 krb5_keyblock **key)
1376 krb5_keytab_key_proc_args *args = rk_UNCONST(keyseed);
1377 krb5_keytab keytab = args->keytab;
1378 krb5_principal principal = args->principal;
1379 krb5_error_code ret;
1380 krb5_keytab real_keytab;
1381 krb5_keytab_entry entry;
1384 krb5_kt_default(context, &real_keytab);
1386 real_keytab = keytab;
1388 ret = krb5_kt_get_entry (context, real_keytab, principal,
1389 0, enctype, &entry);
1392 krb5_kt_close (context, real_keytab);
1397 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
1398 krb5_kt_free_entry(context, &entry);
1404 * Set the keytab to use for authentication.
1406 * @param context a Kerberos 5 context.
1407 * @param ctx ctx krb5_init_creds_context context.
1408 * @param keytab the keytab to read the key from.
1410 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1411 * @ingroup krb5_credential
1414 krb5_error_code KRB5_LIB_FUNCTION
1415 krb5_init_creds_set_keytab(krb5_context context,
1416 krb5_init_creds_context ctx,
1419 krb5_keytab_key_proc_args *a;
1421 a = malloc(sizeof(*a));
1423 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1427 a->principal = ctx->cred.client;
1430 ctx->keytab_data = a;
1431 ctx->keyseed = (void *)a;
1432 ctx->keyproc = keytab_key_proc;
1437 static krb5_error_code
1438 keyblock_key_proc(krb5_context context, krb5_enctype enctype,
1439 krb5_const_pointer keyseed,
1440 krb5_salt salt, krb5_data *s2kparms,
1441 krb5_keyblock **key)
1443 return krb5_copy_keyblock (context, keyseed, key);
1446 krb5_error_code KRB5_LIB_FUNCTION
1447 krb5_init_creds_set_keyblock(krb5_context context,
1448 krb5_init_creds_context ctx,
1449 krb5_keyblock *keyblock)
1451 ctx->keyseed = (void *)keyblock;
1452 ctx->keyproc = keyblock_key_proc;
1458 * The core loop if krb5_get_init_creds() function family. Create the
1459 * packets and have the caller send them off to the KDC.
1461 * If the caller want all work been done for them, use
1462 * krb5_init_creds_get() instead.
1464 * @param context a Kerberos 5 context.
1465 * @param ctx ctx krb5_init_creds_context context.
1466 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
1467 * @param out reply to KDC.
1468 * @param hostinfo KDC address info, first round it can be NULL.
1469 * @param flags status of the round, if 1 is set, continue one more round.
1471 * @return 0 for success, or an Kerberos 5 error code, see
1472 * krb5_get_error_message().
1474 * @ingroup krb5_credential
1477 krb5_error_code KRB5_LIB_FUNCTION
1478 krb5_init_creds_step(krb5_context context,
1479 krb5_init_creds_context ctx,
1482 krb5_krbhst_info *hostinfo,
1483 unsigned int *flags)
1485 krb5_error_code ret;
1489 krb5_data_zero(out);
1491 if (ctx->as_req.req_body.cname == NULL) {
1492 ret = init_as_req(context, ctx->flags, &ctx->cred,
1493 ctx->addrs, ctx->etypes, &ctx->as_req);
1495 free_init_creds_ctx(context, ctx);
1500 #define MAX_PA_COUNTER 10
1501 if (ctx->pa_counter > MAX_PA_COUNTER) {
1502 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1503 N_("Looping %d times while getting "
1504 "initial credentials", ""),
1506 return KRB5_GET_IN_TKT_LOOP;
1510 /* Lets process the input packet */
1511 if (in && in->length) {
1514 memset(&rep, 0, sizeof(rep));
1516 ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size);
1518 krb5_keyblock *key = NULL;
1519 unsigned eflags = EXTRACT_TICKET_AS_REQ;
1521 if (ctx->flags.canonicalize) {
1522 eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1523 eflags |= EXTRACT_TICKET_MATCH_REALM;
1525 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1526 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1528 ret = process_pa_data_to_key(context, ctx, &ctx->cred,
1529 &ctx->as_req, &rep.kdc_rep, hostinfo, &key);
1531 free_AS_REP(&rep.kdc_rep);
1535 ret = _krb5_extract_ticket(context,
1540 KRB5_KU_AS_REP_ENC_PART,
1546 krb5_free_keyblock(context, key);
1551 ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part);
1553 free_AS_REP(&rep.kdc_rep);
1554 free_EncASRepPart(&rep.enc_part);
1559 /* let's try to parse it as a KRB-ERROR */
1561 free_KRB_ERROR(&ctx->error);
1563 ret = krb5_rd_error(context, in, &ctx->error);
1564 if(ret && in->length && ((char*)in->data)[0] == 4)
1565 ret = KRB5KRB_AP_ERR_V4_REPLY;
1569 ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred);
1572 * If no preauth was set and KDC requires it, give it one
1576 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1578 free_METHOD_DATA(&ctx->md);
1579 memset(&ctx->md, 0, sizeof(ctx->md));
1581 if (ctx->error.e_data) {
1582 ret = decode_METHOD_DATA(ctx->error.e_data->data,
1583 ctx->error.e_data->length,
1587 krb5_set_error_message(context, ret,
1588 N_("Failed to decode METHOD-DATA", ""));
1590 krb5_set_error_message(context, ret,
1591 N_("Preauth required but no preauth "
1592 "options send by KDC", ""));
1594 } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) {
1596 * Try adapt to timeskrew when we are using pre-auth, and
1597 * if there was a time skew, try again.
1599 krb5_set_real_time(context, ctx->error.stime, -1);
1600 if (context->kdc_sec_offset)
1602 } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) {
1603 /* client referal to a new realm */
1604 if (ctx->error.crealm == NULL) {
1605 krb5_set_error_message(context, ret,
1606 N_("Got a client referral, not but no realm", ""));
1609 ret = krb5_principal_set_realm(context,
1611 *ctx->error.crealm);
1618 if (ctx->as_req.padata) {
1619 free_METHOD_DATA(ctx->as_req.padata);
1620 free(ctx->as_req.padata);
1621 ctx->as_req.padata = NULL;
1624 /* Set a new nonce. */
1625 ctx->as_req.req_body.nonce = ctx->nonce;
1627 /* fill_in_md_data */
1628 ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx,
1629 &ctx->md, &ctx->as_req.padata,
1630 ctx->prompter, ctx->prompter_data);
1634 krb5_data_free(&ctx->req_buffer);
1636 ASN1_MALLOC_ENCODE(AS_REQ,
1637 ctx->req_buffer.data, ctx->req_buffer.length,
1638 &ctx->as_req, &len, ret);
1641 if(len != ctx->req_buffer.length)
1642 krb5_abortx(context, "internal error in ASN.1 encoder");
1644 out->data = ctx->req_buffer.data;
1645 out->length = ctx->req_buffer.length;
1655 * Extract the newly acquired credentials from krb5_init_creds_context
1658 * @param context A Kerberos 5 context.
1660 * @param cred credentials, free with krb5_free_cred_contents().
1662 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
1665 krb5_error_code KRB5_LIB_FUNCTION
1666 krb5_init_creds_get_creds(krb5_context context,
1667 krb5_init_creds_context ctx,
1670 return krb5_copy_creds_contents(context, &ctx->cred, cred);
1674 * Get the last error from the transaction.
1676 * @return Returns 0 or an error code
1678 * @ingroup krb5_credential
1681 krb5_error_code KRB5_LIB_FUNCTION
1682 krb5_init_creds_get_error(krb5_context context,
1683 krb5_init_creds_context ctx,
1686 krb5_error_code ret;
1688 ret = copy_KRB_ERROR(&ctx->error, error);
1690 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
1696 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
1698 * @param context A Kerberos 5 context.
1699 * @param ctx The krb5_init_creds_context to free.
1701 * @ingroup krb5_credential
1704 void KRB5_LIB_FUNCTION
1705 krb5_init_creds_free(krb5_context context,
1706 krb5_init_creds_context ctx)
1708 free_init_creds_ctx(context, ctx);
1713 * Get new credentials as setup by the krb5_init_creds_context.
1715 * @param context A Kerberos 5 context.
1716 * @param ctx The krb5_init_creds_context to process.
1718 * @ingroup krb5_credential
1721 krb5_error_code KRB5_LIB_FUNCTION
1722 krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx)
1724 krb5_sendto_ctx stctx = NULL;
1725 krb5_krbhst_info *hostinfo = NULL;
1726 krb5_error_code ret;
1728 unsigned int flags = 0;
1730 krb5_data_zero(&in);
1731 krb5_data_zero(&out);
1733 ret = krb5_sendto_ctx_alloc(context, &stctx);
1736 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1740 ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags);
1741 krb5_data_free(&in);
1745 if ((flags & 1) == 0)
1748 ret = krb5_sendto_context (context, stctx, &out,
1749 ctx->cred.client->realm, &in);
1757 krb5_sendto_ctx_free(context, stctx);
1763 * Get new credentials using password.
1765 * @ingroup krb5_credential
1769 krb5_error_code KRB5_LIB_FUNCTION
1770 krb5_get_init_creds_password(krb5_context context,
1772 krb5_principal client,
1773 const char *password,
1774 krb5_prompter_fct prompter,
1776 krb5_deltat start_time,
1777 const char *in_tkt_service,
1778 krb5_get_init_creds_opt *options)
1780 krb5_init_creds_context ctx;
1782 krb5_error_code ret;
1786 ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx);
1790 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1794 if (prompter != NULL && ctx->password == NULL && password == NULL) {
1796 krb5_data password_data;
1799 krb5_unparse_name (context, client, &p);
1800 asprintf (&q, "%s's Password: ", p);
1803 password_data.data = buf;
1804 password_data.length = sizeof(buf);
1806 prompt.reply = &password_data;
1807 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1809 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
1812 memset (buf, 0, sizeof(buf));
1813 ret = KRB5_LIBOS_PWDINTR;
1814 krb5_clear_error_message (context);
1817 password = password_data.data;
1821 ret = krb5_init_creds_set_password(context, ctx, password);
1826 ret = krb5_init_creds_get(context, ctx);
1829 process_last_request(context, options, ctx);
1832 if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) {
1835 /* try to avoid recursion */
1836 if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0)
1839 /* don't try to change password where then where none */
1840 if (prompter == NULL)
1843 ret = change_password (context,
1854 krb5_init_creds_free(context, ctx);
1860 krb5_init_creds_get_creds(context, ctx, creds);
1863 krb5_init_creds_free(context, ctx);
1865 memset(buf, 0, sizeof(buf));
1870 * Get new credentials using keyblock.
1872 * @ingroup krb5_credential
1875 krb5_error_code KRB5_LIB_FUNCTION
1876 krb5_get_init_creds_keyblock(krb5_context context,
1878 krb5_principal client,
1879 krb5_keyblock *keyblock,
1880 krb5_deltat start_time,
1881 const char *in_tkt_service,
1882 krb5_get_init_creds_opt *options)
1884 krb5_init_creds_context ctx;
1885 krb5_error_code ret;
1887 memset(creds, 0, sizeof(*creds));
1889 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
1893 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1897 ret = krb5_init_creds_set_keyblock(context, ctx, keyblock);
1901 ret = krb5_init_creds_get(context, ctx);
1904 process_last_request(context, options, ctx);
1908 krb5_init_creds_get_creds(context, ctx, creds);
1911 krb5_init_creds_free(context, ctx);
1917 * Get new credentials using keytab.
1919 * @ingroup krb5_credential
1922 krb5_error_code KRB5_LIB_FUNCTION
1923 krb5_get_init_creds_keytab(krb5_context context,
1925 krb5_principal client,
1927 krb5_deltat start_time,
1928 const char *in_tkt_service,
1929 krb5_get_init_creds_opt *options)
1931 krb5_init_creds_context ctx;
1932 krb5_error_code ret;
1934 memset(creds, 0, sizeof(*creds));
1936 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
1940 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1944 ret = krb5_init_creds_set_keytab(context, ctx, keytab);
1948 ret = krb5_init_creds_get(context, ctx);
1950 process_last_request(context, options, ctx);
1954 krb5_init_creds_get_creds(context, ctx, creds);
1957 krb5_init_creds_free(context, ctx);