Windows: Enable weak crypto by default
[heimdal.git] / lib / krb5 / init_creds_pw.c
blob869687aa63b5a6e40d77103fcdb4bfc2ee82beb0
1 /*
2 * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Portions Copyright (c) 2009 Apple Inc. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "krb5_locl.h"
38 typedef struct krb5_get_init_creds_ctx {
39 KDCOptions flags;
40 krb5_creds cred;
41 krb5_addresses *addrs;
42 krb5_enctype *etypes;
43 krb5_preauthtype *pre_auth_types;
44 char *in_tkt_service;
45 unsigned nonce;
46 unsigned pk_nonce;
48 krb5_data req_buffer;
49 AS_REQ as_req;
50 int pa_counter;
52 /* password and keytab_data is freed on completion */
53 char *password;
54 krb5_keytab_key_proc_args *keytab_data;
56 krb5_pointer *keyseed;
57 krb5_s2k_proc keyproc;
59 krb5_get_init_creds_tristate req_pac;
61 krb5_pk_init_ctx pk_init_ctx;
62 int ic_flags;
64 int used_pa_types;
65 #define USED_PKINIT 1
66 #define USED_PKINIT_W2K 2
67 #define USED_ENC_TS_GUESS 4
68 #define USED_ENC_TS_INFO 8
70 METHOD_DATA md;
71 KRB_ERROR error;
72 AS_REP as_rep;
73 EncKDCRepPart enc_part;
75 krb5_prompter_fct prompter;
76 void *prompter_data;
78 struct pa_info_data *ppaid;
80 } krb5_get_init_creds_ctx;
83 struct pa_info_data {
84 krb5_enctype etype;
85 krb5_salt salt;
86 krb5_data *s2kparams;
89 static void
90 free_paid(krb5_context context, struct pa_info_data *ppaid)
92 krb5_free_salt(context, ppaid->salt);
93 if (ppaid->s2kparams)
94 krb5_free_data(context, ppaid->s2kparams);
97 static krb5_error_code KRB5_CALLCONV
98 default_s2k_func(krb5_context context, krb5_enctype type,
99 krb5_const_pointer keyseed,
100 krb5_salt salt, krb5_data *s2kparms,
101 krb5_keyblock **key)
103 krb5_error_code ret;
104 krb5_data password;
105 krb5_data opaque;
107 _krb5_debug(context, 5, "krb5_get_init_creds: using default_s2k_func");
109 password.data = rk_UNCONST(keyseed);
110 password.length = strlen(keyseed);
111 if (s2kparms)
112 opaque = *s2kparms;
113 else
114 krb5_data_zero(&opaque);
116 *key = malloc(sizeof(**key));
117 if (*key == NULL)
118 return ENOMEM;
119 ret = krb5_string_to_key_data_salt_opaque(context, type, password,
120 salt, opaque, *key);
121 if (ret) {
122 free(*key);
123 *key = NULL;
125 return ret;
128 static void
129 free_init_creds_ctx(krb5_context context, krb5_init_creds_context ctx)
131 if (ctx->etypes)
132 free(ctx->etypes);
133 if (ctx->pre_auth_types)
134 free (ctx->pre_auth_types);
135 if (ctx->in_tkt_service)
136 free(ctx->in_tkt_service);
137 if (ctx->keytab_data)
138 free(ctx->keytab_data);
139 if (ctx->password) {
140 memset(ctx->password, 0, strlen(ctx->password));
141 free(ctx->password);
143 krb5_data_free(&ctx->req_buffer);
144 krb5_free_cred_contents(context, &ctx->cred);
145 free_METHOD_DATA(&ctx->md);
146 free_AS_REP(&ctx->as_rep);
147 free_EncKDCRepPart(&ctx->enc_part);
148 free_KRB_ERROR(&ctx->error);
149 free_AS_REQ(&ctx->as_req);
150 if (ctx->ppaid) {
151 free_paid(context, ctx->ppaid);
152 free(ctx->ppaid);
154 memset(ctx, 0, sizeof(*ctx));
157 static int
158 get_config_time (krb5_context context,
159 const char *realm,
160 const char *name,
161 int def)
163 int ret;
165 ret = krb5_config_get_time (context, NULL,
166 "realms",
167 realm,
168 name,
169 NULL);
170 if (ret >= 0)
171 return ret;
172 ret = krb5_config_get_time (context, NULL,
173 "libdefaults",
174 name,
175 NULL);
176 if (ret >= 0)
177 return ret;
178 return def;
181 static krb5_error_code
182 init_cred (krb5_context context,
183 krb5_creds *cred,
184 krb5_principal client,
185 krb5_deltat start_time,
186 krb5_get_init_creds_opt *options)
188 krb5_error_code ret;
189 int tmp;
190 krb5_timestamp now;
192 krb5_timeofday (context, &now);
194 memset (cred, 0, sizeof(*cred));
196 if (client)
197 krb5_copy_principal(context, client, &cred->client);
198 else {
199 ret = krb5_get_default_principal (context,
200 &cred->client);
201 if (ret)
202 goto out;
205 if (start_time)
206 cred->times.starttime = now + start_time;
208 if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE)
209 tmp = options->tkt_life;
210 else
211 tmp = 10 * 60 * 60;
212 cred->times.endtime = now + tmp;
214 if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE) &&
215 options->renew_life > 0) {
216 cred->times.renew_till = now + options->renew_life;
219 return 0;
221 out:
222 krb5_free_cred_contents (context, cred);
223 return ret;
227 * Print a message (str) to the user about the expiration in `lr'
230 static void
231 report_expiration (krb5_context context,
232 krb5_prompter_fct prompter,
233 krb5_data *data,
234 const char *str,
235 time_t now)
237 char *p = NULL;
239 if (asprintf(&p, "%s%s", str, ctime(&now)) < 0 || p == NULL)
240 return;
241 (*prompter)(context, data, NULL, p, 0, NULL);
242 free(p);
246 * Check the context, and in the case there is a expiration warning,
247 * use the prompter to print the warning.
249 * @param context A Kerberos 5 context.
250 * @param options An GIC options structure
251 * @param ctx The krb5_init_creds_context check for expiration.
254 static krb5_error_code
255 process_last_request(krb5_context context,
256 krb5_get_init_creds_opt *options,
257 krb5_init_creds_context ctx)
259 krb5_const_realm realm;
260 LastReq *lr;
261 krb5_boolean reported = FALSE;
262 krb5_timestamp sec;
263 time_t t;
264 size_t i;
267 * First check if there is a API consumer.
270 realm = krb5_principal_get_realm (context, ctx->cred.client);
271 lr = &ctx->enc_part.last_req;
273 if (options && options->opt_private && options->opt_private->lr.func) {
274 krb5_last_req_entry **lre;
276 lre = calloc(lr->len + 1, sizeof(**lre));
277 if (lre == NULL) {
278 krb5_set_error_message(context, ENOMEM,
279 N_("malloc: out of memory", ""));
280 return ENOMEM;
282 for (i = 0; i < lr->len; i++) {
283 lre[i] = calloc(1, sizeof(*lre[i]));
284 if (lre[i] == NULL)
285 break;
286 lre[i]->lr_type = lr->val[i].lr_type;
287 lre[i]->value = lr->val[i].lr_value;
290 (*options->opt_private->lr.func)(context, lre,
291 options->opt_private->lr.ctx);
293 for (i = 0; i < lr->len; i++)
294 free(lre[i]);
295 free(lre);
299 * Now check if we should prompt the user
302 if (ctx->prompter == NULL)
303 return 0;
305 krb5_timeofday (context, &sec);
307 t = sec + get_config_time (context,
308 realm,
309 "warn_pwexpire",
310 7 * 24 * 60 * 60);
312 for (i = 0; i < lr->len; ++i) {
313 if (lr->val[i].lr_value <= t) {
314 switch (abs(lr->val[i].lr_type)) {
315 case LR_PW_EXPTIME :
316 report_expiration(context, ctx->prompter,
317 ctx->prompter_data,
318 "Your password will expire at ",
319 lr->val[i].lr_value);
320 reported = TRUE;
321 break;
322 case LR_ACCT_EXPTIME :
323 report_expiration(context, ctx->prompter,
324 ctx->prompter_data,
325 "Your account will expire at ",
326 lr->val[i].lr_value);
327 reported = TRUE;
328 break;
333 if (!reported
334 && ctx->enc_part.key_expiration
335 && *ctx->enc_part.key_expiration <= t) {
336 report_expiration(context, ctx->prompter,
337 ctx->prompter_data,
338 "Your password/account will expire at ",
339 *ctx->enc_part.key_expiration);
341 return 0;
344 static krb5_addresses no_addrs = { 0, NULL };
346 static krb5_error_code
347 get_init_creds_common(krb5_context context,
348 krb5_principal client,
349 krb5_deltat start_time,
350 krb5_get_init_creds_opt *options,
351 krb5_init_creds_context ctx)
353 krb5_get_init_creds_opt *default_opt = NULL;
354 krb5_error_code ret;
355 krb5_enctype *etypes;
356 krb5_preauthtype *pre_auth_types;
358 memset(ctx, 0, sizeof(*ctx));
360 if (options == NULL) {
361 const char *realm = krb5_principal_get_realm(context, client);
363 krb5_get_init_creds_opt_alloc (context, &default_opt);
364 options = default_opt;
365 krb5_get_init_creds_opt_set_default_flags(context, NULL, realm, options);
368 if (options->opt_private) {
369 if (options->opt_private->password) {
370 ret = krb5_init_creds_set_password(context, ctx,
371 options->opt_private->password);
372 if (ret)
373 goto out;
376 ctx->keyproc = options->opt_private->key_proc;
377 ctx->req_pac = options->opt_private->req_pac;
378 ctx->pk_init_ctx = options->opt_private->pk_init_ctx;
379 ctx->ic_flags = options->opt_private->flags;
380 } else
381 ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET;
383 if (ctx->keyproc == NULL)
384 ctx->keyproc = default_s2k_func;
386 /* Enterprise name implicitly turns on canonicalize */
387 if ((ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE) ||
388 krb5_principal_get_type(context, client) == KRB5_NT_ENTERPRISE_PRINCIPAL)
389 ctx->flags.canonicalize = 1;
391 ctx->pre_auth_types = NULL;
392 ctx->addrs = NULL;
393 ctx->etypes = NULL;
394 ctx->pre_auth_types = NULL;
396 ret = init_cred(context, &ctx->cred, client, start_time, options);
397 if (ret) {
398 if (default_opt)
399 krb5_get_init_creds_opt_free(context, default_opt);
400 return ret;
403 ret = krb5_init_creds_set_service(context, ctx, NULL);
404 if (ret)
405 goto out;
407 if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE)
408 ctx->flags.forwardable = options->forwardable;
410 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE)
411 ctx->flags.proxiable = options->proxiable;
413 if (start_time)
414 ctx->flags.postdated = 1;
415 if (ctx->cred.times.renew_till)
416 ctx->flags.renewable = 1;
417 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) {
418 ctx->addrs = options->address_list;
419 } else if (options->opt_private) {
420 switch (options->opt_private->addressless) {
421 case KRB5_INIT_CREDS_TRISTATE_UNSET:
422 #if KRB5_ADDRESSLESS_DEFAULT == TRUE
423 ctx->addrs = &no_addrs;
424 #else
425 ctx->addrs = NULL;
426 #endif
427 break;
428 case KRB5_INIT_CREDS_TRISTATE_FALSE:
429 ctx->addrs = NULL;
430 break;
431 case KRB5_INIT_CREDS_TRISTATE_TRUE:
432 ctx->addrs = &no_addrs;
433 break;
436 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) {
437 if (ctx->etypes)
438 free(ctx->etypes);
440 etypes = malloc((options->etype_list_length + 1)
441 * sizeof(krb5_enctype));
442 if (etypes == NULL) {
443 ret = ENOMEM;
444 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
445 goto out;
447 memcpy (etypes, options->etype_list,
448 options->etype_list_length * sizeof(krb5_enctype));
449 etypes[options->etype_list_length] = ETYPE_NULL;
450 ctx->etypes = etypes;
452 if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) {
453 pre_auth_types = malloc((options->preauth_list_length + 1)
454 * sizeof(krb5_preauthtype));
455 if (pre_auth_types == NULL) {
456 ret = ENOMEM;
457 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
458 goto out;
460 memcpy (pre_auth_types, options->preauth_list,
461 options->preauth_list_length * sizeof(krb5_preauthtype));
462 pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE;
463 ctx->pre_auth_types = pre_auth_types;
465 if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS)
466 ctx->flags.request_anonymous = options->anonymous;
467 if (default_opt)
468 krb5_get_init_creds_opt_free(context, default_opt);
469 return 0;
470 out:
471 if (default_opt)
472 krb5_get_init_creds_opt_free(context, default_opt);
473 return ret;
476 static krb5_error_code
477 change_password (krb5_context context,
478 krb5_principal client,
479 const char *password,
480 char *newpw,
481 size_t newpw_sz,
482 krb5_prompter_fct prompter,
483 void *data,
484 krb5_get_init_creds_opt *old_options)
486 krb5_prompt prompts[2];
487 krb5_error_code ret;
488 krb5_creds cpw_cred;
489 char buf1[BUFSIZ], buf2[BUFSIZ];
490 krb5_data password_data[2];
491 int result_code;
492 krb5_data result_code_string;
493 krb5_data result_string;
494 char *p;
495 krb5_get_init_creds_opt *options;
497 memset (&cpw_cred, 0, sizeof(cpw_cred));
499 ret = krb5_get_init_creds_opt_alloc(context, &options);
500 if (ret)
501 return ret;
502 krb5_get_init_creds_opt_set_tkt_life (options, 60);
503 krb5_get_init_creds_opt_set_forwardable (options, FALSE);
504 krb5_get_init_creds_opt_set_proxiable (options, FALSE);
505 if (old_options && old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)
506 krb5_get_init_creds_opt_set_preauth_list (options,
507 old_options->preauth_list,
508 old_options->preauth_list_length);
510 krb5_data_zero (&result_code_string);
511 krb5_data_zero (&result_string);
513 ret = krb5_get_init_creds_password (context,
514 &cpw_cred,
515 client,
516 password,
517 prompter,
518 data,
520 "kadmin/changepw",
521 options);
522 krb5_get_init_creds_opt_free(context, options);
523 if (ret)
524 goto out;
526 for(;;) {
527 password_data[0].data = buf1;
528 password_data[0].length = sizeof(buf1);
530 prompts[0].hidden = 1;
531 prompts[0].prompt = "New password: ";
532 prompts[0].reply = &password_data[0];
533 prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD;
535 password_data[1].data = buf2;
536 password_data[1].length = sizeof(buf2);
538 prompts[1].hidden = 1;
539 prompts[1].prompt = "Repeat new password: ";
540 prompts[1].reply = &password_data[1];
541 prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN;
543 ret = (*prompter) (context, data, NULL, "Changing password",
544 2, prompts);
545 if (ret) {
546 memset (buf1, 0, sizeof(buf1));
547 memset (buf2, 0, sizeof(buf2));
548 goto out;
551 if (strcmp (buf1, buf2) == 0)
552 break;
553 memset (buf1, 0, sizeof(buf1));
554 memset (buf2, 0, sizeof(buf2));
557 ret = krb5_set_password (context,
558 &cpw_cred,
559 buf1,
560 client,
561 &result_code,
562 &result_code_string,
563 &result_string);
564 if (ret)
565 goto out;
566 if (asprintf(&p, "%s: %.*s\n",
567 result_code ? "Error" : "Success",
568 (int)result_string.length,
569 result_string.length > 0 ? (char*)result_string.data : "") < 0)
571 ret = ENOMEM;
572 goto out;
575 /* return the result */
576 (*prompter) (context, data, NULL, p, 0, NULL);
578 free (p);
579 if (result_code == 0) {
580 strlcpy (newpw, buf1, newpw_sz);
581 ret = 0;
582 } else {
583 ret = ENOTTY;
584 krb5_set_error_message(context, ret,
585 N_("failed changing password", ""));
588 out:
589 memset (buf1, 0, sizeof(buf1));
590 memset (buf2, 0, sizeof(buf2));
591 krb5_data_free (&result_string);
592 krb5_data_free (&result_code_string);
593 krb5_free_cred_contents (context, &cpw_cred);
594 return ret;
598 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
599 krb5_keyblock_key_proc (krb5_context context,
600 krb5_keytype type,
601 krb5_data *salt,
602 krb5_const_pointer keyseed,
603 krb5_keyblock **key)
605 return krb5_copy_keyblock (context, keyseed, key);
612 static krb5_error_code
613 init_as_req (krb5_context context,
614 KDCOptions opts,
615 const krb5_creds *creds,
616 const krb5_addresses *addrs,
617 const krb5_enctype *etypes,
618 AS_REQ *a)
620 krb5_error_code ret;
622 memset(a, 0, sizeof(*a));
624 a->pvno = 5;
625 a->msg_type = krb_as_req;
626 a->req_body.kdc_options = opts;
627 a->req_body.cname = malloc(sizeof(*a->req_body.cname));
628 if (a->req_body.cname == NULL) {
629 ret = ENOMEM;
630 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
631 goto fail;
633 a->req_body.sname = malloc(sizeof(*a->req_body.sname));
634 if (a->req_body.sname == NULL) {
635 ret = ENOMEM;
636 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
637 goto fail;
640 ret = _krb5_principal2principalname (a->req_body.cname, creds->client);
641 if (ret)
642 goto fail;
643 ret = copy_Realm(&creds->client->realm, &a->req_body.realm);
644 if (ret)
645 goto fail;
647 ret = _krb5_principal2principalname (a->req_body.sname, creds->server);
648 if (ret)
649 goto fail;
651 if(creds->times.starttime) {
652 a->req_body.from = malloc(sizeof(*a->req_body.from));
653 if (a->req_body.from == NULL) {
654 ret = ENOMEM;
655 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
656 goto fail;
658 *a->req_body.from = creds->times.starttime;
660 if(creds->times.endtime){
661 ALLOC(a->req_body.till, 1);
662 *a->req_body.till = creds->times.endtime;
664 if(creds->times.renew_till){
665 a->req_body.rtime = malloc(sizeof(*a->req_body.rtime));
666 if (a->req_body.rtime == NULL) {
667 ret = ENOMEM;
668 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
669 goto fail;
671 *a->req_body.rtime = creds->times.renew_till;
673 a->req_body.nonce = 0;
674 ret = krb5_init_etype (context,
675 &a->req_body.etype.len,
676 &a->req_body.etype.val,
677 etypes);
678 if (ret)
679 goto fail;
682 * This means no addresses
685 if (addrs && addrs->len == 0) {
686 a->req_body.addresses = NULL;
687 } else {
688 a->req_body.addresses = malloc(sizeof(*a->req_body.addresses));
689 if (a->req_body.addresses == NULL) {
690 ret = ENOMEM;
691 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
692 goto fail;
695 if (addrs)
696 ret = krb5_copy_addresses(context, addrs, a->req_body.addresses);
697 else {
698 ret = krb5_get_all_client_addrs (context, a->req_body.addresses);
699 if(ret == 0 && a->req_body.addresses->len == 0) {
700 free(a->req_body.addresses);
701 a->req_body.addresses = NULL;
704 if (ret)
705 goto fail;
708 a->req_body.enc_authorization_data = NULL;
709 a->req_body.additional_tickets = NULL;
711 a->padata = NULL;
713 return 0;
714 fail:
715 free_AS_REQ(a);
716 memset(a, 0, sizeof(*a));
717 return ret;
721 static krb5_error_code
722 set_paid(struct pa_info_data *paid, krb5_context context,
723 krb5_enctype etype,
724 krb5_salttype salttype, void *salt_string, size_t salt_len,
725 krb5_data *s2kparams)
727 paid->etype = etype;
728 paid->salt.salttype = salttype;
729 paid->salt.saltvalue.data = malloc(salt_len + 1);
730 if (paid->salt.saltvalue.data == NULL) {
731 krb5_clear_error_message(context);
732 return ENOMEM;
734 memcpy(paid->salt.saltvalue.data, salt_string, salt_len);
735 ((char *)paid->salt.saltvalue.data)[salt_len] = '\0';
736 paid->salt.saltvalue.length = salt_len;
737 if (s2kparams) {
738 krb5_error_code ret;
740 ret = krb5_copy_data(context, s2kparams, &paid->s2kparams);
741 if (ret) {
742 krb5_clear_error_message(context);
743 krb5_free_salt(context, paid->salt);
744 return ret;
746 } else
747 paid->s2kparams = NULL;
749 return 0;
752 static struct pa_info_data *
753 pa_etype_info2(krb5_context context,
754 const krb5_principal client,
755 const AS_REQ *asreq,
756 struct pa_info_data *paid,
757 heim_octet_string *data)
759 krb5_error_code ret;
760 ETYPE_INFO2 e;
761 size_t sz;
762 int i, j;
764 memset(&e, 0, sizeof(e));
765 ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz);
766 if (ret)
767 goto out;
768 if (e.len == 0)
769 goto out;
770 for (j = 0; j < asreq->req_body.etype.len; j++) {
771 for (i = 0; i < e.len; i++) {
772 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
773 krb5_salt salt;
774 if (e.val[i].salt == NULL)
775 ret = krb5_get_pw_salt(context, client, &salt);
776 else {
777 salt.saltvalue.data = *e.val[i].salt;
778 salt.saltvalue.length = strlen(*e.val[i].salt);
779 ret = 0;
781 if (ret == 0)
782 ret = set_paid(paid, context, e.val[i].etype,
783 KRB5_PW_SALT,
784 salt.saltvalue.data,
785 salt.saltvalue.length,
786 e.val[i].s2kparams);
787 if (e.val[i].salt == NULL)
788 krb5_free_salt(context, salt);
789 if (ret == 0) {
790 free_ETYPE_INFO2(&e);
791 return paid;
796 out:
797 free_ETYPE_INFO2(&e);
798 return NULL;
801 static struct pa_info_data *
802 pa_etype_info(krb5_context context,
803 const krb5_principal client,
804 const AS_REQ *asreq,
805 struct pa_info_data *paid,
806 heim_octet_string *data)
808 krb5_error_code ret;
809 ETYPE_INFO e;
810 size_t sz;
811 int i, j;
813 memset(&e, 0, sizeof(e));
814 ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz);
815 if (ret)
816 goto out;
817 if (e.len == 0)
818 goto out;
819 for (j = 0; j < asreq->req_body.etype.len; j++) {
820 for (i = 0; i < e.len; i++) {
821 if (asreq->req_body.etype.val[j] == e.val[i].etype) {
822 krb5_salt salt;
823 salt.salttype = KRB5_PW_SALT;
824 if (e.val[i].salt == NULL)
825 ret = krb5_get_pw_salt(context, client, &salt);
826 else {
827 salt.saltvalue = *e.val[i].salt;
828 ret = 0;
830 if (e.val[i].salttype)
831 salt.salttype = *e.val[i].salttype;
832 if (ret == 0) {
833 ret = set_paid(paid, context, e.val[i].etype,
834 salt.salttype,
835 salt.saltvalue.data,
836 salt.saltvalue.length,
837 NULL);
838 if (e.val[i].salt == NULL)
839 krb5_free_salt(context, salt);
841 if (ret == 0) {
842 free_ETYPE_INFO(&e);
843 return paid;
848 out:
849 free_ETYPE_INFO(&e);
850 return NULL;
853 static struct pa_info_data *
854 pa_pw_or_afs3_salt(krb5_context context,
855 const krb5_principal client,
856 const AS_REQ *asreq,
857 struct pa_info_data *paid,
858 heim_octet_string *data)
860 krb5_error_code ret;
861 if (paid->etype == ENCTYPE_NULL)
862 return NULL;
863 ret = set_paid(paid, context,
864 paid->etype,
865 paid->salt.salttype,
866 data->data,
867 data->length,
868 NULL);
869 if (ret)
870 return NULL;
871 return paid;
875 struct pa_info {
876 krb5_preauthtype type;
877 struct pa_info_data *(*salt_info)(krb5_context,
878 const krb5_principal,
879 const AS_REQ *,
880 struct pa_info_data *,
881 heim_octet_string *);
884 static struct pa_info pa_prefs[] = {
885 { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 },
886 { KRB5_PADATA_ETYPE_INFO, pa_etype_info },
887 { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt },
888 { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt }
891 static PA_DATA *
892 find_pa_data(const METHOD_DATA *md, int type)
894 int i;
895 if (md == NULL)
896 return NULL;
897 for (i = 0; i < md->len; i++)
898 if (md->val[i].padata_type == type)
899 return &md->val[i];
900 return NULL;
903 static struct pa_info_data *
904 process_pa_info(krb5_context context,
905 const krb5_principal client,
906 const AS_REQ *asreq,
907 struct pa_info_data *paid,
908 METHOD_DATA *md)
910 struct pa_info_data *p = NULL;
911 int i;
913 for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) {
914 PA_DATA *pa = find_pa_data(md, pa_prefs[i].type);
915 if (pa == NULL)
916 continue;
917 paid->salt.salttype = pa_prefs[i].type;
918 p = (*pa_prefs[i].salt_info)(context, client, asreq,
919 paid, &pa->padata_value);
921 return p;
924 static krb5_error_code
925 make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md,
926 krb5_enctype etype, krb5_keyblock *key)
928 PA_ENC_TS_ENC p;
929 unsigned char *buf;
930 size_t buf_size;
931 size_t len;
932 EncryptedData encdata;
933 krb5_error_code ret;
934 int32_t usec;
935 int usec2;
936 krb5_crypto crypto;
938 krb5_us_timeofday (context, &p.patimestamp, &usec);
939 usec2 = usec;
940 p.pausec = &usec2;
942 ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret);
943 if (ret)
944 return ret;
945 if(buf_size != len)
946 krb5_abortx(context, "internal error in ASN.1 encoder");
948 ret = krb5_crypto_init(context, key, 0, &crypto);
949 if (ret) {
950 free(buf);
951 return ret;
953 ret = krb5_encrypt_EncryptedData(context,
954 crypto,
955 KRB5_KU_PA_ENC_TIMESTAMP,
956 buf,
957 len,
959 &encdata);
960 free(buf);
961 krb5_crypto_destroy(context, crypto);
962 if (ret)
963 return ret;
965 ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret);
966 free_EncryptedData(&encdata);
967 if (ret)
968 return ret;
969 if(buf_size != len)
970 krb5_abortx(context, "internal error in ASN.1 encoder");
972 ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len);
973 if (ret)
974 free(buf);
975 return ret;
978 static krb5_error_code
979 add_enc_ts_padata(krb5_context context,
980 METHOD_DATA *md,
981 krb5_principal client,
982 krb5_s2k_proc keyproc,
983 krb5_const_pointer keyseed,
984 krb5_enctype *enctypes,
985 unsigned netypes,
986 krb5_salt *salt,
987 krb5_data *s2kparams)
989 krb5_error_code ret;
990 krb5_salt salt2;
991 krb5_enctype *ep;
992 int i;
994 if(salt == NULL) {
995 /* default to standard salt */
996 ret = krb5_get_pw_salt (context, client, &salt2);
997 if (ret)
998 return ret;
999 salt = &salt2;
1001 if (!enctypes) {
1002 enctypes = context->etypes;
1003 netypes = 0;
1004 for (ep = enctypes; *ep != ETYPE_NULL; ep++)
1005 netypes++;
1008 for (i = 0; i < netypes; ++i) {
1009 krb5_keyblock *key;
1011 _krb5_debug(context, 5, "krb5_get_init_creds: using ENC-TS with enctype %d", enctypes[i]);
1013 ret = (*keyproc)(context, enctypes[i], keyseed,
1014 *salt, s2kparams, &key);
1015 if (ret)
1016 continue;
1017 ret = make_pa_enc_timestamp (context, md, enctypes[i], key);
1018 krb5_free_keyblock (context, key);
1019 if (ret)
1020 return ret;
1022 if(salt == &salt2)
1023 krb5_free_salt(context, salt2);
1024 return 0;
1027 static krb5_error_code
1028 pa_data_to_md_ts_enc(krb5_context context,
1029 const AS_REQ *a,
1030 const krb5_principal client,
1031 krb5_get_init_creds_ctx *ctx,
1032 struct pa_info_data *ppaid,
1033 METHOD_DATA *md)
1035 if (ctx->keyproc == NULL || ctx->keyseed == NULL)
1036 return 0;
1038 if (ppaid) {
1039 add_enc_ts_padata(context, md, client,
1040 ctx->keyproc, ctx->keyseed,
1041 &ppaid->etype, 1,
1042 &ppaid->salt, ppaid->s2kparams);
1043 } else {
1044 krb5_salt salt;
1046 _krb5_debug(context, 5, "krb5_get_init_creds: pa-info not found, guessing salt");
1048 /* make a v5 salted pa-data */
1049 add_enc_ts_padata(context, md, client,
1050 ctx->keyproc, ctx->keyseed,
1051 a->req_body.etype.val, a->req_body.etype.len,
1052 NULL, NULL);
1054 /* make a v4 salted pa-data */
1055 salt.salttype = KRB5_PW_SALT;
1056 krb5_data_zero(&salt.saltvalue);
1057 add_enc_ts_padata(context, md, client,
1058 ctx->keyproc, ctx->keyseed,
1059 a->req_body.etype.val, a->req_body.etype.len,
1060 &salt, NULL);
1062 return 0;
1065 static krb5_error_code
1066 pa_data_to_key_plain(krb5_context context,
1067 const krb5_principal client,
1068 krb5_get_init_creds_ctx *ctx,
1069 krb5_salt salt,
1070 krb5_data *s2kparams,
1071 krb5_enctype etype,
1072 krb5_keyblock **key)
1074 krb5_error_code ret;
1076 ret = (*ctx->keyproc)(context, etype, ctx->keyseed,
1077 salt, s2kparams, key);
1078 return ret;
1082 static krb5_error_code
1083 pa_data_to_md_pkinit(krb5_context context,
1084 const AS_REQ *a,
1085 const krb5_principal client,
1086 int win2k,
1087 krb5_get_init_creds_ctx *ctx,
1088 METHOD_DATA *md)
1090 if (ctx->pk_init_ctx == NULL)
1091 return 0;
1092 #ifdef PKINIT
1093 return _krb5_pk_mk_padata(context,
1094 ctx->pk_init_ctx,
1095 ctx->ic_flags,
1096 win2k,
1097 &a->req_body,
1098 ctx->pk_nonce,
1099 md);
1100 #else
1101 krb5_set_error_message(context, EINVAL,
1102 N_("no support for PKINIT compiled in", ""));
1103 return EINVAL;
1104 #endif
1107 static krb5_error_code
1108 pa_data_add_pac_request(krb5_context context,
1109 krb5_get_init_creds_ctx *ctx,
1110 METHOD_DATA *md)
1112 size_t len, length;
1113 krb5_error_code ret;
1114 PA_PAC_REQUEST req;
1115 void *buf;
1117 switch (ctx->req_pac) {
1118 case KRB5_INIT_CREDS_TRISTATE_UNSET:
1119 return 0; /* don't bother */
1120 case KRB5_INIT_CREDS_TRISTATE_TRUE:
1121 req.include_pac = 1;
1122 break;
1123 case KRB5_INIT_CREDS_TRISTATE_FALSE:
1124 req.include_pac = 0;
1127 ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length,
1128 &req, &len, ret);
1129 if (ret)
1130 return ret;
1131 if(len != length)
1132 krb5_abortx(context, "internal error in ASN.1 encoder");
1134 ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len);
1135 if (ret)
1136 free(buf);
1138 return 0;
1142 * Assumes caller always will free `out_md', even on error.
1145 static krb5_error_code
1146 process_pa_data_to_md(krb5_context context,
1147 const krb5_creds *creds,
1148 const AS_REQ *a,
1149 krb5_get_init_creds_ctx *ctx,
1150 METHOD_DATA *in_md,
1151 METHOD_DATA **out_md,
1152 krb5_prompter_fct prompter,
1153 void *prompter_data)
1155 krb5_error_code ret;
1157 ALLOC(*out_md, 1);
1158 if (*out_md == NULL) {
1159 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1160 return ENOMEM;
1162 (*out_md)->len = 0;
1163 (*out_md)->val = NULL;
1165 if (_krb5_have_debug(context, 5)) {
1166 unsigned i;
1167 _krb5_debug(context, 5, "KDC send %d patypes", in_md->len);
1168 for (i = 0; i < in_md->len; i++)
1169 _krb5_debug(context, 5, "KDC send PA-DATA type: %d", in_md->val[i].padata_type);
1173 * Make sure we don't sent both ENC-TS and PK-INIT pa data, no
1174 * need to expose our password protecting our PKCS12 key.
1177 if (ctx->pk_init_ctx) {
1179 _krb5_debug(context, 5, "krb5_get_init_creds: "
1180 "prepareing PKINIT padata (%s)",
1181 (ctx->used_pa_types & USED_PKINIT_W2K) ? "win2k" : "ietf");
1183 if (ctx->used_pa_types & USED_PKINIT_W2K) {
1184 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1185 "Already tried pkinit, looping");
1186 return KRB5_GET_IN_TKT_LOOP;
1189 ret = pa_data_to_md_pkinit(context, a, creds->client,
1190 (ctx->used_pa_types & USED_PKINIT),
1191 ctx, *out_md);
1192 if (ret)
1193 return ret;
1195 if (ctx->used_pa_types & USED_PKINIT)
1196 ctx->used_pa_types |= USED_PKINIT_W2K;
1197 else
1198 ctx->used_pa_types |= USED_PKINIT;
1200 } else if (in_md->len != 0) {
1201 struct pa_info_data *paid, *ppaid;
1202 unsigned flag;
1204 paid = calloc(1, sizeof(*paid));
1206 paid->etype = ENCTYPE_NULL;
1207 ppaid = process_pa_info(context, creds->client, a, paid, in_md);
1209 if (ppaid)
1210 flag = USED_ENC_TS_INFO;
1211 else
1212 flag = USED_ENC_TS_GUESS;
1214 if (ctx->used_pa_types & flag) {
1215 if (ppaid)
1216 free_paid(context, ppaid);
1217 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1218 "Already tried ENC-TS-%s, looping",
1219 flag == USED_ENC_TS_INFO ? "info" : "guess");
1220 return KRB5_GET_IN_TKT_LOOP;
1223 pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md);
1225 ctx->used_pa_types |= flag;
1227 if (ppaid) {
1228 if (ctx->ppaid) {
1229 free_paid(context, ctx->ppaid);
1230 free(ctx->ppaid);
1232 ctx->ppaid = ppaid;
1233 } else
1234 free(paid);
1237 pa_data_add_pac_request(context, ctx, *out_md);
1239 if ((*out_md)->len == 0) {
1240 free(*out_md);
1241 *out_md = NULL;
1244 return 0;
1247 static krb5_error_code
1248 process_pa_data_to_key(krb5_context context,
1249 krb5_get_init_creds_ctx *ctx,
1250 krb5_creds *creds,
1251 AS_REQ *a,
1252 AS_REP *rep,
1253 const krb5_krbhst_info *hi,
1254 krb5_keyblock **key)
1256 struct pa_info_data paid, *ppaid = NULL;
1257 krb5_error_code ret;
1258 krb5_enctype etype;
1259 PA_DATA *pa;
1261 memset(&paid, 0, sizeof(paid));
1263 etype = rep->enc_part.etype;
1265 if (rep->padata) {
1266 paid.etype = etype;
1267 ppaid = process_pa_info(context, creds->client, a, &paid,
1268 rep->padata);
1270 if (ppaid == NULL)
1271 ppaid = ctx->ppaid;
1272 if (ppaid == NULL) {
1273 ret = krb5_get_pw_salt (context, creds->client, &paid.salt);
1274 if (ret)
1275 return ret;
1276 paid.etype = etype;
1277 paid.s2kparams = NULL;
1278 ppaid = &paid;
1281 pa = NULL;
1282 if (rep->padata) {
1283 int idx = 0;
1284 pa = krb5_find_padata(rep->padata->val,
1285 rep->padata->len,
1286 KRB5_PADATA_PK_AS_REP,
1287 &idx);
1288 if (pa == NULL) {
1289 idx = 0;
1290 pa = krb5_find_padata(rep->padata->val,
1291 rep->padata->len,
1292 KRB5_PADATA_PK_AS_REP_19,
1293 &idx);
1296 if (pa && ctx->pk_init_ctx) {
1297 #ifdef PKINIT
1298 _krb5_debug(context, 5, "krb5_get_init_creds: using PKINIT");
1300 ret = _krb5_pk_rd_pa_reply(context,
1301 a->req_body.realm,
1302 ctx->pk_init_ctx,
1303 etype,
1305 ctx->pk_nonce,
1306 &ctx->req_buffer,
1308 key);
1309 #else
1310 ret = EINVAL;
1311 krb5_set_error_message(context, ret, N_("no support for PKINIT compiled in", ""));
1312 #endif
1313 } else if (ctx->keyseed) {
1314 _krb5_debug(context, 5, "krb5_get_init_creds: using keyproc");
1315 ret = pa_data_to_key_plain(context, creds->client, ctx,
1316 ppaid->salt, ppaid->s2kparams, etype, key);
1317 } else {
1318 ret = EINVAL;
1319 krb5_set_error_message(context, ret, N_("No usable pa data type", ""));
1322 free_paid(context, &paid);
1323 return ret;
1327 * Start a new context to get a new initial credential.
1329 * @param context A Kerberos 5 context.
1330 * @param client The Kerberos principal to get the credential for, if
1331 * NULL is given, the default principal is used as determined by
1332 * krb5_get_default_principal().
1333 * @param prompter
1334 * @param prompter_data
1335 * @param start_time the time the ticket should start to be valid or 0 for now.
1336 * @param options a options structure, can be NULL for default options.
1337 * @param rctx A new allocated free with krb5_init_creds_free().
1339 * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message().
1341 * @ingroup krb5_credential
1344 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1345 krb5_init_creds_init(krb5_context context,
1346 krb5_principal client,
1347 krb5_prompter_fct prompter,
1348 void *prompter_data,
1349 krb5_deltat start_time,
1350 krb5_get_init_creds_opt *options,
1351 krb5_init_creds_context *rctx)
1353 krb5_init_creds_context ctx;
1354 krb5_error_code ret;
1356 *rctx = NULL;
1358 ctx = calloc(1, sizeof(*ctx));
1359 if (ctx == NULL) {
1360 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1361 return ENOMEM;
1364 ret = get_init_creds_common(context, client, start_time, options, ctx);
1365 if (ret) {
1366 free(ctx);
1367 return ret;
1370 /* Set a new nonce. */
1371 krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce));
1372 ctx->nonce &= 0x7fffffff;
1373 /* XXX these just needs to be the same when using Windows PK-INIT */
1374 ctx->pk_nonce = ctx->nonce;
1376 ctx->prompter = prompter;
1377 ctx->prompter_data = prompter_data;
1379 *rctx = ctx;
1381 return ret;
1385 * Sets the service that the is requested. This call is only neede for
1386 * special initial tickets, by default the a krbtgt is fetched in the default realm.
1388 * @param context a Kerberos 5 context.
1389 * @param ctx a krb5_init_creds_context context.
1390 * @param service the service given as a string, for example
1391 * "kadmind/admin". If NULL, the default krbtgt in the clients
1392 * realm is set.
1394 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1395 * @ingroup krb5_credential
1398 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1399 krb5_init_creds_set_service(krb5_context context,
1400 krb5_init_creds_context ctx,
1401 const char *service)
1403 krb5_const_realm client_realm;
1404 krb5_principal principal;
1405 krb5_error_code ret;
1407 client_realm = krb5_principal_get_realm (context, ctx->cred.client);
1409 if (service) {
1410 ret = krb5_parse_name (context, service, &principal);
1411 if (ret)
1412 return ret;
1413 krb5_principal_set_realm (context, principal, client_realm);
1414 } else {
1415 ret = krb5_make_principal(context, &principal,
1416 client_realm, KRB5_TGS_NAME, client_realm,
1417 NULL);
1418 if (ret)
1419 return ret;
1421 krb5_free_principal(context, ctx->cred.server);
1422 ctx->cred.server = principal;
1424 return 0;
1428 * Sets the password that will use for the request.
1430 * @param context a Kerberos 5 context.
1431 * @param ctx ctx krb5_init_creds_context context.
1432 * @param password the password to use.
1434 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1435 * @ingroup krb5_credential
1438 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1439 krb5_init_creds_set_password(krb5_context context,
1440 krb5_init_creds_context ctx,
1441 const char *password)
1443 if (ctx->password) {
1444 memset(ctx->password, 0, strlen(ctx->password));
1445 free(ctx->password);
1447 if (password) {
1448 ctx->password = strdup(password);
1449 if (ctx->password == NULL) {
1450 krb5_set_error_message(context, ENOMEM, N_("malloc: out of memory", ""));
1451 return ENOMEM;
1453 ctx->keyseed = (void *) ctx->password;
1454 } else {
1455 ctx->keyseed = NULL;
1456 ctx->password = NULL;
1459 return 0;
1462 static krb5_error_code KRB5_CALLCONV
1463 keytab_key_proc(krb5_context context, krb5_enctype enctype,
1464 krb5_const_pointer keyseed,
1465 krb5_salt salt, krb5_data *s2kparms,
1466 krb5_keyblock **key)
1468 krb5_keytab_key_proc_args *args = rk_UNCONST(keyseed);
1469 krb5_keytab keytab = args->keytab;
1470 krb5_principal principal = args->principal;
1471 krb5_error_code ret;
1472 krb5_keytab real_keytab;
1473 krb5_keytab_entry entry;
1475 if(keytab == NULL)
1476 krb5_kt_default(context, &real_keytab);
1477 else
1478 real_keytab = keytab;
1480 ret = krb5_kt_get_entry (context, real_keytab, principal,
1481 0, enctype, &entry);
1483 if (keytab == NULL)
1484 krb5_kt_close (context, real_keytab);
1486 if (ret)
1487 return ret;
1489 ret = krb5_copy_keyblock (context, &entry.keyblock, key);
1490 krb5_kt_free_entry(context, &entry);
1491 return ret;
1496 * Set the keytab to use for authentication.
1498 * @param context a Kerberos 5 context.
1499 * @param ctx ctx krb5_init_creds_context context.
1500 * @param keytab the keytab to read the key from.
1502 * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message().
1503 * @ingroup krb5_credential
1506 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1507 krb5_init_creds_set_keytab(krb5_context context,
1508 krb5_init_creds_context ctx,
1509 krb5_keytab keytab)
1511 krb5_keytab_key_proc_args *a;
1512 krb5_keytab_entry entry;
1513 krb5_kt_cursor cursor;
1514 krb5_enctype *etypes = NULL;
1515 krb5_error_code ret;
1516 size_t netypes = 0;
1517 int kvno = 0;
1519 a = malloc(sizeof(*a));
1520 if (a == NULL) {
1521 krb5_set_error_message(context, ENOMEM,
1522 N_("malloc: out of memory", ""));
1523 return ENOMEM;
1526 a->principal = ctx->cred.client;
1527 a->keytab = keytab;
1529 ctx->keytab_data = a;
1530 ctx->keyseed = (void *)a;
1531 ctx->keyproc = keytab_key_proc;
1534 * We need to the KDC what enctypes we support for this keytab,
1535 * esp if the keytab is really a password based entry, then the
1536 * KDC might have more enctypes in the database then what we have
1537 * in the keytab.
1540 ret = krb5_kt_start_seq_get(context, keytab, &cursor);
1541 if(ret)
1542 goto out;
1544 while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){
1545 void *ptr;
1547 if (!krb5_principal_compare(context, entry.principal, ctx->cred.client))
1548 goto next;
1550 /* check if we ahve this kvno already */
1551 if (entry.vno > kvno) {
1552 /* remove old list of etype */
1553 if (etypes)
1554 free(etypes);
1555 etypes = NULL;
1556 netypes = 0;
1557 kvno = entry.vno;
1558 } else if (entry.vno != kvno)
1559 goto next;
1561 /* check if enctype is supported */
1562 if (krb5_enctype_valid(context, entry.keyblock.keytype) != 0)
1563 goto next;
1565 /* add enctype to supported list */
1566 ptr = realloc(etypes, sizeof(etypes[0]) * (netypes + 2));
1567 if (ptr == NULL)
1568 goto next;
1570 etypes = ptr;
1571 etypes[netypes] = entry.keyblock.keytype;
1572 etypes[netypes + 1] = ETYPE_NULL;
1573 netypes++;
1574 next:
1575 krb5_kt_free_entry(context, &entry);
1577 krb5_kt_end_seq_get(context, keytab, &cursor);
1579 if (etypes) {
1580 if (ctx->etypes)
1581 free(ctx->etypes);
1582 ctx->etypes = etypes;
1585 out:
1586 return 0;
1589 static krb5_error_code KRB5_CALLCONV
1590 keyblock_key_proc(krb5_context context, krb5_enctype enctype,
1591 krb5_const_pointer keyseed,
1592 krb5_salt salt, krb5_data *s2kparms,
1593 krb5_keyblock **key)
1595 return krb5_copy_keyblock (context, keyseed, key);
1598 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1599 krb5_init_creds_set_keyblock(krb5_context context,
1600 krb5_init_creds_context ctx,
1601 krb5_keyblock *keyblock)
1603 ctx->keyseed = (void *)keyblock;
1604 ctx->keyproc = keyblock_key_proc;
1606 return 0;
1610 * The core loop if krb5_get_init_creds() function family. Create the
1611 * packets and have the caller send them off to the KDC.
1613 * If the caller want all work been done for them, use
1614 * krb5_init_creds_get() instead.
1616 * @param context a Kerberos 5 context.
1617 * @param ctx ctx krb5_init_creds_context context.
1618 * @param in input data from KDC, first round it should be reset by krb5_data_zer().
1619 * @param out reply to KDC.
1620 * @param hostinfo KDC address info, first round it can be NULL.
1621 * @param flags status of the round, if
1622 * KRB5_INIT_CREDS_STEP_FLAG_CONTINUE is set, continue one more round.
1624 * @return 0 for success, or an Kerberos 5 error code, see
1625 * krb5_get_error_message().
1627 * @ingroup krb5_credential
1630 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1631 krb5_init_creds_step(krb5_context context,
1632 krb5_init_creds_context ctx,
1633 krb5_data *in,
1634 krb5_data *out,
1635 krb5_krbhst_info *hostinfo,
1636 unsigned int *flags)
1638 krb5_error_code ret;
1639 size_t len;
1640 size_t size;
1642 krb5_data_zero(out);
1644 if (ctx->as_req.req_body.cname == NULL) {
1645 ret = init_as_req(context, ctx->flags, &ctx->cred,
1646 ctx->addrs, ctx->etypes, &ctx->as_req);
1647 if (ret) {
1648 free_init_creds_ctx(context, ctx);
1649 return ret;
1653 #define MAX_PA_COUNTER 10
1654 if (ctx->pa_counter > MAX_PA_COUNTER) {
1655 krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP,
1656 N_("Looping %d times while getting "
1657 "initial credentials", ""),
1658 ctx->pa_counter);
1659 return KRB5_GET_IN_TKT_LOOP;
1661 ctx->pa_counter++;
1663 _krb5_debug(context, 5, "krb5_get_init_creds: loop %d", ctx->pa_counter);
1665 /* Lets process the input packet */
1666 if (in && in->length) {
1667 krb5_kdc_rep rep;
1669 memset(&rep, 0, sizeof(rep));
1671 _krb5_debug(context, 5, "krb5_get_init_creds: processing input");
1673 ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size);
1674 if (ret == 0) {
1675 krb5_keyblock *key = NULL;
1676 unsigned eflags = EXTRACT_TICKET_AS_REQ | EXTRACT_TICKET_TIMESYNC;
1678 if (ctx->flags.canonicalize) {
1679 eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH;
1680 eflags |= EXTRACT_TICKET_MATCH_REALM;
1682 if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK)
1683 eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH;
1685 ret = process_pa_data_to_key(context, ctx, &ctx->cred,
1686 &ctx->as_req, &rep.kdc_rep, hostinfo, &key);
1687 if (ret) {
1688 free_AS_REP(&rep.kdc_rep);
1689 goto out;
1692 _krb5_debug(context, 5, "krb5_get_init_creds: extracting ticket");
1694 ret = _krb5_extract_ticket(context,
1695 &rep,
1696 &ctx->cred,
1697 key,
1698 NULL,
1699 KRB5_KU_AS_REP_ENC_PART,
1700 NULL,
1701 ctx->nonce,
1702 eflags,
1703 NULL,
1704 NULL);
1705 krb5_free_keyblock(context, key);
1707 *flags = 0;
1709 if (ret == 0)
1710 ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part);
1712 free_AS_REP(&rep.kdc_rep);
1713 free_EncASRepPart(&rep.enc_part);
1715 return ret;
1717 } else {
1718 /* let's try to parse it as a KRB-ERROR */
1720 _krb5_debug(context, 5, "krb5_get_init_creds: got an error");
1722 free_KRB_ERROR(&ctx->error);
1724 ret = krb5_rd_error(context, in, &ctx->error);
1725 if(ret && in->length && ((char*)in->data)[0] == 4)
1726 ret = KRB5KRB_AP_ERR_V4_REPLY;
1727 if (ret) {
1728 _krb5_debug(context, 5, "krb5_get_init_creds: failed to read error");
1729 goto out;
1732 ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred);
1734 _krb5_debug(context, 5, "krb5_get_init_creds: KRB-ERROR %d", ret);
1737 * If no preauth was set and KDC requires it, give it one
1738 * more try.
1741 if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) {
1743 free_METHOD_DATA(&ctx->md);
1744 memset(&ctx->md, 0, sizeof(ctx->md));
1746 if (ctx->error.e_data) {
1747 ret = decode_METHOD_DATA(ctx->error.e_data->data,
1748 ctx->error.e_data->length,
1749 &ctx->md,
1750 NULL);
1751 if (ret)
1752 krb5_set_error_message(context, ret,
1753 N_("Failed to decode METHOD-DATA", ""));
1754 } else {
1755 krb5_set_error_message(context, ret,
1756 N_("Preauth required but no preauth "
1757 "options send by KDC", ""));
1759 } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) {
1761 * Try adapt to timeskrew when we are using pre-auth, and
1762 * if there was a time skew, try again.
1764 krb5_set_real_time(context, ctx->error.stime, -1);
1765 if (context->kdc_sec_offset)
1766 ret = 0;
1768 _krb5_debug(context, 10, "init_creds: err skew updateing kdc offset to %d",
1769 context->kdc_sec_offset);
1771 ctx->used_pa_types = 0;
1773 } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) {
1774 /* client referal to a new realm */
1776 if (ctx->error.crealm == NULL) {
1777 krb5_set_error_message(context, ret,
1778 N_("Got a client referral, not but no realm", ""));
1779 goto out;
1781 _krb5_debug(context, 5,
1782 "krb5_get_init_creds: got referal to realm %s",
1783 *ctx->error.crealm);
1785 ret = krb5_principal_set_realm(context,
1786 ctx->cred.client,
1787 *ctx->error.crealm);
1789 ctx->used_pa_types = 0;
1791 if (ret)
1792 goto out;
1796 if (ctx->as_req.padata) {
1797 free_METHOD_DATA(ctx->as_req.padata);
1798 free(ctx->as_req.padata);
1799 ctx->as_req.padata = NULL;
1802 /* Set a new nonce. */
1803 ctx->as_req.req_body.nonce = ctx->nonce;
1805 /* fill_in_md_data */
1806 ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx,
1807 &ctx->md, &ctx->as_req.padata,
1808 ctx->prompter, ctx->prompter_data);
1809 if (ret)
1810 goto out;
1812 krb5_data_free(&ctx->req_buffer);
1814 ASN1_MALLOC_ENCODE(AS_REQ,
1815 ctx->req_buffer.data, ctx->req_buffer.length,
1816 &ctx->as_req, &len, ret);
1817 if (ret)
1818 goto out;
1819 if(len != ctx->req_buffer.length)
1820 krb5_abortx(context, "internal error in ASN.1 encoder");
1822 out->data = ctx->req_buffer.data;
1823 out->length = ctx->req_buffer.length;
1825 *flags = KRB5_INIT_CREDS_STEP_FLAG_CONTINUE;
1827 return 0;
1828 out:
1829 return ret;
1833 * Extract the newly acquired credentials from krb5_init_creds_context
1834 * context.
1836 * @param context A Kerberos 5 context.
1837 * @param ctx
1838 * @param cred credentials, free with krb5_free_cred_contents().
1840 * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message().
1843 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1844 krb5_init_creds_get_creds(krb5_context context,
1845 krb5_init_creds_context ctx,
1846 krb5_creds *cred)
1848 return krb5_copy_creds_contents(context, &ctx->cred, cred);
1852 * Get the last error from the transaction.
1854 * @return Returns 0 or an error code
1856 * @ingroup krb5_credential
1859 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1860 krb5_init_creds_get_error(krb5_context context,
1861 krb5_init_creds_context ctx,
1862 KRB_ERROR *error)
1864 krb5_error_code ret;
1866 ret = copy_KRB_ERROR(&ctx->error, error);
1867 if (ret)
1868 krb5_set_error_message(context, ret, N_("malloc: out of memory", ""));
1870 return ret;
1874 * Free the krb5_init_creds_context allocated by krb5_init_creds_init().
1876 * @param context A Kerberos 5 context.
1877 * @param ctx The krb5_init_creds_context to free.
1879 * @ingroup krb5_credential
1882 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
1883 krb5_init_creds_free(krb5_context context,
1884 krb5_init_creds_context ctx)
1886 free_init_creds_ctx(context, ctx);
1887 free(ctx);
1891 * Get new credentials as setup by the krb5_init_creds_context.
1893 * @param context A Kerberos 5 context.
1894 * @param ctx The krb5_init_creds_context to process.
1896 * @ingroup krb5_credential
1899 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1900 krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx)
1902 krb5_sendto_ctx stctx = NULL;
1903 krb5_krbhst_info *hostinfo = NULL;
1904 krb5_error_code ret;
1905 krb5_data in, out;
1906 unsigned int flags = 0;
1908 krb5_data_zero(&in);
1909 krb5_data_zero(&out);
1911 ret = krb5_sendto_ctx_alloc(context, &stctx);
1912 if (ret)
1913 goto out;
1914 krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL);
1916 while (1) {
1917 flags = 0;
1918 ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags);
1919 krb5_data_free(&in);
1920 if (ret)
1921 goto out;
1923 if ((flags & 1) == 0)
1924 break;
1926 ret = krb5_sendto_context (context, stctx, &out,
1927 ctx->cred.client->realm, &in);
1928 if (ret)
1929 goto out;
1933 out:
1934 if (stctx)
1935 krb5_sendto_ctx_free(context, stctx);
1937 return ret;
1941 * Get new credentials using password.
1943 * @ingroup krb5_credential
1947 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
1948 krb5_get_init_creds_password(krb5_context context,
1949 krb5_creds *creds,
1950 krb5_principal client,
1951 const char *password,
1952 krb5_prompter_fct prompter,
1953 void *data,
1954 krb5_deltat start_time,
1955 const char *in_tkt_service,
1956 krb5_get_init_creds_opt *options)
1958 krb5_init_creds_context ctx;
1959 char buf[BUFSIZ];
1960 krb5_error_code ret;
1961 int chpw = 0;
1963 again:
1964 ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx);
1965 if (ret)
1966 goto out;
1968 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
1969 if (ret)
1970 goto out;
1972 if (prompter != NULL && ctx->password == NULL && password == NULL) {
1973 krb5_prompt prompt;
1974 krb5_data password_data;
1975 char *p, *q;
1977 krb5_unparse_name (context, client, &p);
1978 asprintf (&q, "%s's Password: ", p);
1979 free (p);
1980 prompt.prompt = q;
1981 password_data.data = buf;
1982 password_data.length = sizeof(buf);
1983 prompt.hidden = 1;
1984 prompt.reply = &password_data;
1985 prompt.type = KRB5_PROMPT_TYPE_PASSWORD;
1987 ret = (*prompter) (context, data, NULL, NULL, 1, &prompt);
1988 free (q);
1989 if (ret) {
1990 memset (buf, 0, sizeof(buf));
1991 ret = KRB5_LIBOS_PWDINTR;
1992 krb5_clear_error_message (context);
1993 goto out;
1995 password = password_data.data;
1998 if (password) {
1999 ret = krb5_init_creds_set_password(context, ctx, password);
2000 if (ret)
2001 goto out;
2004 ret = krb5_init_creds_get(context, ctx);
2006 if (ret == 0)
2007 process_last_request(context, options, ctx);
2010 if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) {
2011 char buf2[1024];
2013 /* try to avoid recursion */
2014 if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0)
2015 goto out;
2017 /* don't try to change password where then where none */
2018 if (prompter == NULL)
2019 goto out;
2021 ret = change_password (context,
2022 client,
2023 ctx->password,
2024 buf2,
2025 sizeof(buf),
2026 prompter,
2027 data,
2028 options);
2029 if (ret)
2030 goto out;
2031 chpw = 1;
2032 krb5_init_creds_free(context, ctx);
2033 goto again;
2036 out:
2037 if (ret == 0)
2038 krb5_init_creds_get_creds(context, ctx, creds);
2040 if (ctx)
2041 krb5_init_creds_free(context, ctx);
2043 memset(buf, 0, sizeof(buf));
2044 return ret;
2048 * Get new credentials using keyblock.
2050 * @ingroup krb5_credential
2053 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2054 krb5_get_init_creds_keyblock(krb5_context context,
2055 krb5_creds *creds,
2056 krb5_principal client,
2057 krb5_keyblock *keyblock,
2058 krb5_deltat start_time,
2059 const char *in_tkt_service,
2060 krb5_get_init_creds_opt *options)
2062 krb5_init_creds_context ctx;
2063 krb5_error_code ret;
2065 memset(creds, 0, sizeof(*creds));
2067 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2068 if (ret)
2069 goto out;
2071 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2072 if (ret)
2073 goto out;
2075 ret = krb5_init_creds_set_keyblock(context, ctx, keyblock);
2076 if (ret)
2077 goto out;
2079 ret = krb5_init_creds_get(context, ctx);
2081 if (ret == 0)
2082 process_last_request(context, options, ctx);
2084 out:
2085 if (ret == 0)
2086 krb5_init_creds_get_creds(context, ctx, creds);
2088 if (ctx)
2089 krb5_init_creds_free(context, ctx);
2091 return ret;
2095 * Get new credentials using keytab.
2097 * @ingroup krb5_credential
2100 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
2101 krb5_get_init_creds_keytab(krb5_context context,
2102 krb5_creds *creds,
2103 krb5_principal client,
2104 krb5_keytab keytab,
2105 krb5_deltat start_time,
2106 const char *in_tkt_service,
2107 krb5_get_init_creds_opt *options)
2109 krb5_init_creds_context ctx;
2110 krb5_error_code ret;
2112 memset(creds, 0, sizeof(*creds));
2114 ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx);
2115 if (ret)
2116 goto out;
2118 ret = krb5_init_creds_set_service(context, ctx, in_tkt_service);
2119 if (ret)
2120 goto out;
2122 ret = krb5_init_creds_set_keytab(context, ctx, keytab);
2123 if (ret)
2124 goto out;
2126 ret = krb5_init_creds_get(context, ctx);
2127 if (ret == 0)
2128 process_last_request(context, options, ctx);
2130 out:
2131 if (ret == 0)
2132 krb5_init_creds_get_creds(context, ctx, creds);
2134 if (ctx)
2135 krb5_init_creds_free(context, ctx);
2137 return ret;