1 1.1 elric /* $NetBSD: init_creds_pw.c,v 1.4 2023/06/19 21:41:44 christos Exp $ */ 2 1.1 elric 3 1.1 elric /* 4 1.1 elric * Copyright (c) 1997 - 2008 Kungliga Tekniska Hgskolan 5 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden). 6 1.1 elric * All rights reserved. 7 1.1 elric * 8 1.1 elric * Portions Copyright (c) 2009 Apple Inc. All rights reserved. 9 1.1 elric * 10 1.1 elric * Redistribution and use in source and binary forms, with or without 11 1.1 elric * modification, are permitted provided that the following conditions 12 1.1 elric * are met: 13 1.1 elric * 14 1.1 elric * 1. Redistributions of source code must retain the above copyright 15 1.1 elric * notice, this list of conditions and the following disclaimer. 16 1.1 elric * 17 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright 18 1.1 elric * notice, this list of conditions and the following disclaimer in the 19 1.1 elric * documentation and/or other materials provided with the distribution. 20 1.1 elric * 21 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors 22 1.1 elric * may be used to endorse or promote products derived from this software 23 1.1 elric * without specific prior written permission. 24 1.1 elric * 25 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 26 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 29 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 1.1 elric * SUCH DAMAGE. 36 1.1 elric */ 37 1.1 elric 38 1.1 elric #include "krb5_locl.h" 39 1.2 christos #ifndef WIN32 40 1.2 christos #include <heim-ipc.h> 41 1.2 christos #endif /* WIN32 */ 42 1.1 elric 43 1.1 elric typedef struct krb5_get_init_creds_ctx { 44 1.1 elric KDCOptions flags; 45 1.1 elric krb5_creds cred; 46 1.1 elric krb5_addresses *addrs; 47 1.1 elric krb5_enctype *etypes; 48 1.1 elric krb5_preauthtype *pre_auth_types; 49 1.1 elric char *in_tkt_service; 50 1.1 elric unsigned nonce; 51 1.1 elric unsigned pk_nonce; 52 1.1 elric 53 1.1 elric krb5_data req_buffer; 54 1.1 elric AS_REQ as_req; 55 1.1 elric int pa_counter; 56 1.1 elric 57 1.1 elric /* password and keytab_data is freed on completion */ 58 1.1 elric char *password; 59 1.1 elric krb5_keytab_key_proc_args *keytab_data; 60 1.1 elric 61 1.1 elric krb5_pointer *keyseed; 62 1.1 elric krb5_s2k_proc keyproc; 63 1.1 elric 64 1.1 elric krb5_get_init_creds_tristate req_pac; 65 1.1 elric 66 1.1 elric krb5_pk_init_ctx pk_init_ctx; 67 1.1 elric int ic_flags; 68 1.1 elric 69 1.2 christos struct { 70 1.2 christos unsigned change_password:1; 71 1.2 christos } runflags; 72 1.2 christos 73 1.1 elric int used_pa_types; 74 1.1 elric #define USED_PKINIT 1 75 1.1 elric #define USED_PKINIT_W2K 2 76 1.1 elric #define USED_ENC_TS_GUESS 4 77 1.1 elric #define USED_ENC_TS_INFO 8 78 1.1 elric 79 1.1 elric METHOD_DATA md; 80 1.1 elric KRB_ERROR error; 81 1.1 elric AS_REP as_rep; 82 1.1 elric EncKDCRepPart enc_part; 83 1.2 christos 84 1.1 elric krb5_prompter_fct prompter; 85 1.1 elric void *prompter_data; 86 1.1 elric 87 1.1 elric struct pa_info_data *ppaid; 88 1.2 christos struct fast_state { 89 1.2 christos enum PA_FX_FAST_REQUEST_enum type; 90 1.2 christos unsigned int flags; 91 1.2 christos #define KRB5_FAST_REPLY_KEY_USE_TO_ENCRYPT_THE_REPLY 1 92 1.2 christos #define KRB5_FAST_REPLY_KEY_USE_IN_TRANSACTION 2 93 1.2 christos #define KRB5_FAST_KDC_REPLY_KEY_REPLACED 4 94 1.2 christos #define KRB5_FAST_REPLY_REPLY_VERIFED 8 95 1.2 christos #define KRB5_FAST_STRONG 16 96 1.2 christos #define KRB5_FAST_EXPECTED 32 /* in exchange with KDC, fast was discovered */ 97 1.2 christos #define KRB5_FAST_REQUIRED 64 /* fast required by action of caller */ 98 1.2 christos #define KRB5_FAST_DISABLED 128 99 1.2 christos #define KRB5_FAST_AP_ARMOR_SERVICE 256 100 1.2 christos krb5_keyblock *reply_key; 101 1.2 christos krb5_ccache armor_ccache; 102 1.2 christos krb5_principal armor_service; 103 1.2 christos krb5_crypto armor_crypto; 104 1.2 christos krb5_keyblock armor_key; 105 1.2 christos krb5_keyblock *strengthen_key; 106 1.2 christos } fast_state; 107 1.1 elric } krb5_get_init_creds_ctx; 108 1.1 elric 109 1.1 elric 110 1.1 elric struct pa_info_data { 111 1.1 elric krb5_enctype etype; 112 1.1 elric krb5_salt salt; 113 1.1 elric krb5_data *s2kparams; 114 1.1 elric }; 115 1.1 elric 116 1.1 elric static void 117 1.1 elric free_paid(krb5_context context, struct pa_info_data *ppaid) 118 1.1 elric { 119 1.1 elric krb5_free_salt(context, ppaid->salt); 120 1.1 elric if (ppaid->s2kparams) 121 1.1 elric krb5_free_data(context, ppaid->s2kparams); 122 1.1 elric } 123 1.1 elric 124 1.1 elric static krb5_error_code KRB5_CALLCONV 125 1.1 elric default_s2k_func(krb5_context context, krb5_enctype type, 126 1.1 elric krb5_const_pointer keyseed, 127 1.1 elric krb5_salt salt, krb5_data *s2kparms, 128 1.1 elric krb5_keyblock **key) 129 1.1 elric { 130 1.1 elric krb5_error_code ret; 131 1.1 elric krb5_data password; 132 1.1 elric krb5_data opaque; 133 1.1 elric 134 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: using default_s2k_func"); 135 1.1 elric 136 1.1 elric password.data = rk_UNCONST(keyseed); 137 1.1 elric password.length = strlen(keyseed); 138 1.1 elric if (s2kparms) 139 1.1 elric opaque = *s2kparms; 140 1.1 elric else 141 1.1 elric krb5_data_zero(&opaque); 142 1.1 elric 143 1.1 elric *key = malloc(sizeof(**key)); 144 1.1 elric if (*key == NULL) 145 1.1 elric return ENOMEM; 146 1.1 elric ret = krb5_string_to_key_data_salt_opaque(context, type, password, 147 1.1 elric salt, opaque, *key); 148 1.1 elric if (ret) { 149 1.1 elric free(*key); 150 1.1 elric *key = NULL; 151 1.1 elric } 152 1.1 elric return ret; 153 1.1 elric } 154 1.1 elric 155 1.1 elric static void 156 1.1 elric free_init_creds_ctx(krb5_context context, krb5_init_creds_context ctx) 157 1.1 elric { 158 1.1 elric if (ctx->etypes) 159 1.1 elric free(ctx->etypes); 160 1.1 elric if (ctx->pre_auth_types) 161 1.1 elric free (ctx->pre_auth_types); 162 1.1 elric if (ctx->in_tkt_service) 163 1.1 elric free(ctx->in_tkt_service); 164 1.1 elric if (ctx->keytab_data) 165 1.1 elric free(ctx->keytab_data); 166 1.1 elric if (ctx->password) { 167 1.3 christos size_t len; 168 1.3 christos len = strlen(ctx->password); 169 1.3 christos memset_s(ctx->password, len, 0, len); 170 1.1 elric free(ctx->password); 171 1.1 elric } 172 1.2 christos /* 173 1.2 christos * FAST state (we don't close the armor_ccache because we might have 174 1.2 christos * to destroy it, and how would we know? also, the caller should 175 1.2 christos * take care of cleaning up the armor_ccache). 176 1.2 christos */ 177 1.2 christos if (ctx->fast_state.armor_service) 178 1.2 christos krb5_free_principal(context, ctx->fast_state.armor_service); 179 1.2 christos if (ctx->fast_state.armor_crypto) 180 1.2 christos krb5_crypto_destroy(context, ctx->fast_state.armor_crypto); 181 1.2 christos if (ctx->fast_state.strengthen_key) 182 1.2 christos krb5_free_keyblock(context, ctx->fast_state.strengthen_key); 183 1.2 christos krb5_free_keyblock_contents(context, &ctx->fast_state.armor_key); 184 1.2 christos 185 1.1 elric krb5_data_free(&ctx->req_buffer); 186 1.1 elric krb5_free_cred_contents(context, &ctx->cred); 187 1.1 elric free_METHOD_DATA(&ctx->md); 188 1.1 elric free_AS_REP(&ctx->as_rep); 189 1.1 elric free_EncKDCRepPart(&ctx->enc_part); 190 1.1 elric free_KRB_ERROR(&ctx->error); 191 1.1 elric free_AS_REQ(&ctx->as_req); 192 1.1 elric if (ctx->ppaid) { 193 1.1 elric free_paid(context, ctx->ppaid); 194 1.1 elric free(ctx->ppaid); 195 1.1 elric } 196 1.3 christos memset_s(ctx, sizeof(*ctx), 0, sizeof(*ctx)); 197 1.1 elric } 198 1.1 elric 199 1.1 elric static int 200 1.1 elric get_config_time (krb5_context context, 201 1.1 elric const char *realm, 202 1.1 elric const char *name, 203 1.1 elric int def) 204 1.1 elric { 205 1.1 elric int ret; 206 1.1 elric 207 1.1 elric ret = krb5_config_get_time (context, NULL, 208 1.1 elric "realms", 209 1.1 elric realm, 210 1.1 elric name, 211 1.1 elric NULL); 212 1.1 elric if (ret >= 0) 213 1.1 elric return ret; 214 1.1 elric ret = krb5_config_get_time (context, NULL, 215 1.1 elric "libdefaults", 216 1.1 elric name, 217 1.1 elric NULL); 218 1.1 elric if (ret >= 0) 219 1.1 elric return ret; 220 1.1 elric return def; 221 1.1 elric } 222 1.1 elric 223 1.1 elric static krb5_error_code 224 1.1 elric init_cred (krb5_context context, 225 1.1 elric krb5_creds *cred, 226 1.1 elric krb5_principal client, 227 1.1 elric krb5_deltat start_time, 228 1.1 elric krb5_get_init_creds_opt *options) 229 1.1 elric { 230 1.1 elric krb5_error_code ret; 231 1.1 elric int tmp; 232 1.1 elric krb5_timestamp now; 233 1.1 elric 234 1.1 elric krb5_timeofday (context, &now); 235 1.1 elric 236 1.1 elric memset (cred, 0, sizeof(*cred)); 237 1.1 elric 238 1.1 elric if (client) 239 1.2 christos ret = krb5_copy_principal(context, client, &cred->client); 240 1.2 christos else 241 1.2 christos ret = krb5_get_default_principal(context, &cred->client); 242 1.2 christos if (ret) 243 1.2 christos goto out; 244 1.1 elric 245 1.1 elric if (start_time) 246 1.1 elric cred->times.starttime = now + start_time; 247 1.1 elric 248 1.1 elric if (options->flags & KRB5_GET_INIT_CREDS_OPT_TKT_LIFE) 249 1.1 elric tmp = options->tkt_life; 250 1.1 elric else 251 1.2 christos tmp = KRB5_TKT_LIFETIME_DEFAULT; 252 1.1 elric cred->times.endtime = now + tmp; 253 1.1 elric 254 1.2 christos if ((options->flags & KRB5_GET_INIT_CREDS_OPT_RENEW_LIFE)) { 255 1.2 christos if (options->renew_life > 0) 256 1.2 christos tmp = options->renew_life; 257 1.2 christos else 258 1.2 christos tmp = KRB5_TKT_RENEW_LIFETIME_DEFAULT; 259 1.2 christos cred->times.renew_till = now + tmp; 260 1.1 elric } 261 1.1 elric 262 1.1 elric return 0; 263 1.1 elric 264 1.1 elric out: 265 1.1 elric krb5_free_cred_contents (context, cred); 266 1.1 elric return ret; 267 1.1 elric } 268 1.1 elric 269 1.1 elric /* 270 1.1 elric * Print a message (str) to the user about the expiration in `lr' 271 1.1 elric */ 272 1.1 elric 273 1.1 elric static void 274 1.1 elric report_expiration (krb5_context context, 275 1.1 elric krb5_prompter_fct prompter, 276 1.1 elric krb5_data *data, 277 1.1 elric const char *str, 278 1.1 elric time_t now) 279 1.1 elric { 280 1.1 elric char *p = NULL; 281 1.1 elric 282 1.1 elric if (asprintf(&p, "%s%s", str, ctime(&now)) < 0 || p == NULL) 283 1.1 elric return; 284 1.1 elric (*prompter)(context, data, NULL, p, 0, NULL); 285 1.1 elric free(p); 286 1.1 elric } 287 1.1 elric 288 1.1 elric /* 289 1.1 elric * Check the context, and in the case there is a expiration warning, 290 1.1 elric * use the prompter to print the warning. 291 1.1 elric * 292 1.1 elric * @param context A Kerberos 5 context. 293 1.1 elric * @param options An GIC options structure 294 1.1 elric * @param ctx The krb5_init_creds_context check for expiration. 295 1.1 elric */ 296 1.1 elric 297 1.2 christos krb5_error_code 298 1.2 christos krb5_process_last_request(krb5_context context, 299 1.2 christos krb5_get_init_creds_opt *options, 300 1.2 christos krb5_init_creds_context ctx) 301 1.1 elric { 302 1.1 elric krb5_const_realm realm; 303 1.1 elric LastReq *lr; 304 1.1 elric krb5_boolean reported = FALSE; 305 1.1 elric krb5_timestamp sec; 306 1.1 elric time_t t; 307 1.1 elric size_t i; 308 1.1 elric 309 1.1 elric /* 310 1.1 elric * First check if there is a API consumer. 311 1.1 elric */ 312 1.1 elric 313 1.1 elric realm = krb5_principal_get_realm (context, ctx->cred.client); 314 1.1 elric lr = &ctx->enc_part.last_req; 315 1.1 elric 316 1.1 elric if (options && options->opt_private && options->opt_private->lr.func) { 317 1.1 elric krb5_last_req_entry **lre; 318 1.1 elric 319 1.2 christos lre = calloc(lr->len + 1, sizeof(*lre)); 320 1.2 christos if (lre == NULL) 321 1.2 christos return krb5_enomem(context); 322 1.1 elric for (i = 0; i < lr->len; i++) { 323 1.1 elric lre[i] = calloc(1, sizeof(*lre[i])); 324 1.1 elric if (lre[i] == NULL) 325 1.1 elric break; 326 1.1 elric lre[i]->lr_type = lr->val[i].lr_type; 327 1.1 elric lre[i]->value = lr->val[i].lr_value; 328 1.1 elric } 329 1.1 elric 330 1.1 elric (*options->opt_private->lr.func)(context, lre, 331 1.1 elric options->opt_private->lr.ctx); 332 1.1 elric 333 1.1 elric for (i = 0; i < lr->len; i++) 334 1.1 elric free(lre[i]); 335 1.1 elric free(lre); 336 1.1 elric } 337 1.1 elric 338 1.1 elric /* 339 1.1 elric * Now check if we should prompt the user 340 1.1 elric */ 341 1.1 elric 342 1.1 elric if (ctx->prompter == NULL) 343 1.1 elric return 0; 344 1.1 elric 345 1.1 elric krb5_timeofday (context, &sec); 346 1.1 elric 347 1.1 elric t = sec + get_config_time (context, 348 1.1 elric realm, 349 1.1 elric "warn_pwexpire", 350 1.1 elric 7 * 24 * 60 * 60); 351 1.1 elric 352 1.1 elric for (i = 0; i < lr->len; ++i) { 353 1.1 elric if (lr->val[i].lr_value <= t) { 354 1.2 christos switch (lr->val[i].lr_type) { 355 1.1 elric case LR_PW_EXPTIME : 356 1.2 christos report_expiration(context, ctx->prompter, 357 1.1 elric ctx->prompter_data, 358 1.1 elric "Your password will expire at ", 359 1.1 elric lr->val[i].lr_value); 360 1.1 elric reported = TRUE; 361 1.1 elric break; 362 1.1 elric case LR_ACCT_EXPTIME : 363 1.2 christos report_expiration(context, ctx->prompter, 364 1.1 elric ctx->prompter_data, 365 1.1 elric "Your account will expire at ", 366 1.1 elric lr->val[i].lr_value); 367 1.1 elric reported = TRUE; 368 1.1 elric break; 369 1.2 christos default: 370 1.2 christos break; 371 1.1 elric } 372 1.1 elric } 373 1.1 elric } 374 1.1 elric 375 1.1 elric if (!reported 376 1.1 elric && ctx->enc_part.key_expiration 377 1.1 elric && *ctx->enc_part.key_expiration <= t) { 378 1.2 christos report_expiration(context, ctx->prompter, 379 1.1 elric ctx->prompter_data, 380 1.1 elric "Your password/account will expire at ", 381 1.1 elric *ctx->enc_part.key_expiration); 382 1.1 elric } 383 1.1 elric return 0; 384 1.1 elric } 385 1.1 elric 386 1.1 elric static krb5_addresses no_addrs = { 0, NULL }; 387 1.1 elric 388 1.1 elric static krb5_error_code 389 1.1 elric get_init_creds_common(krb5_context context, 390 1.1 elric krb5_principal client, 391 1.1 elric krb5_deltat start_time, 392 1.1 elric krb5_get_init_creds_opt *options, 393 1.1 elric krb5_init_creds_context ctx) 394 1.1 elric { 395 1.1 elric krb5_get_init_creds_opt *default_opt = NULL; 396 1.1 elric krb5_error_code ret; 397 1.1 elric krb5_enctype *etypes; 398 1.1 elric krb5_preauthtype *pre_auth_types; 399 1.1 elric 400 1.1 elric memset(ctx, 0, sizeof(*ctx)); 401 1.1 elric 402 1.1 elric if (options == NULL) { 403 1.1 elric const char *realm = krb5_principal_get_realm(context, client); 404 1.1 elric 405 1.1 elric krb5_get_init_creds_opt_alloc (context, &default_opt); 406 1.1 elric options = default_opt; 407 1.1 elric krb5_get_init_creds_opt_set_default_flags(context, NULL, realm, options); 408 1.1 elric } 409 1.1 elric 410 1.1 elric if (options->opt_private) { 411 1.1 elric if (options->opt_private->password) { 412 1.2 christos ret = krb5_init_creds_set_password(context, ctx, 413 1.1 elric options->opt_private->password); 414 1.1 elric if (ret) 415 1.1 elric goto out; 416 1.1 elric } 417 1.1 elric 418 1.1 elric ctx->keyproc = options->opt_private->key_proc; 419 1.1 elric ctx->req_pac = options->opt_private->req_pac; 420 1.1 elric ctx->pk_init_ctx = options->opt_private->pk_init_ctx; 421 1.1 elric ctx->ic_flags = options->opt_private->flags; 422 1.1 elric } else 423 1.1 elric ctx->req_pac = KRB5_INIT_CREDS_TRISTATE_UNSET; 424 1.1 elric 425 1.1 elric if (ctx->keyproc == NULL) 426 1.1 elric ctx->keyproc = default_s2k_func; 427 1.1 elric 428 1.1 elric /* Enterprise name implicitly turns on canonicalize */ 429 1.2 christos if ((ctx->ic_flags & KRB5_INIT_CREDS_CANONICALIZE) || 430 1.1 elric krb5_principal_get_type(context, client) == KRB5_NT_ENTERPRISE_PRINCIPAL) 431 1.1 elric ctx->flags.canonicalize = 1; 432 1.1 elric 433 1.1 elric ctx->pre_auth_types = NULL; 434 1.1 elric ctx->addrs = NULL; 435 1.1 elric ctx->etypes = NULL; 436 1.1 elric ctx->pre_auth_types = NULL; 437 1.1 elric 438 1.1 elric ret = init_cred(context, &ctx->cred, client, start_time, options); 439 1.1 elric if (ret) { 440 1.1 elric if (default_opt) 441 1.1 elric krb5_get_init_creds_opt_free(context, default_opt); 442 1.1 elric return ret; 443 1.1 elric } 444 1.1 elric 445 1.1 elric ret = krb5_init_creds_set_service(context, ctx, NULL); 446 1.1 elric if (ret) 447 1.1 elric goto out; 448 1.1 elric 449 1.1 elric if (options->flags & KRB5_GET_INIT_CREDS_OPT_FORWARDABLE) 450 1.1 elric ctx->flags.forwardable = options->forwardable; 451 1.1 elric 452 1.1 elric if (options->flags & KRB5_GET_INIT_CREDS_OPT_PROXIABLE) 453 1.1 elric ctx->flags.proxiable = options->proxiable; 454 1.1 elric 455 1.1 elric if (start_time) 456 1.1 elric ctx->flags.postdated = 1; 457 1.1 elric if (ctx->cred.times.renew_till) 458 1.1 elric ctx->flags.renewable = 1; 459 1.1 elric if (options->flags & KRB5_GET_INIT_CREDS_OPT_ADDRESS_LIST) { 460 1.1 elric ctx->addrs = options->address_list; 461 1.1 elric } else if (options->opt_private) { 462 1.1 elric switch (options->opt_private->addressless) { 463 1.1 elric case KRB5_INIT_CREDS_TRISTATE_UNSET: 464 1.1 elric #if KRB5_ADDRESSLESS_DEFAULT == TRUE 465 1.1 elric ctx->addrs = &no_addrs; 466 1.1 elric #else 467 1.1 elric ctx->addrs = NULL; 468 1.1 elric #endif 469 1.1 elric break; 470 1.1 elric case KRB5_INIT_CREDS_TRISTATE_FALSE: 471 1.1 elric ctx->addrs = NULL; 472 1.1 elric break; 473 1.1 elric case KRB5_INIT_CREDS_TRISTATE_TRUE: 474 1.1 elric ctx->addrs = &no_addrs; 475 1.1 elric break; 476 1.1 elric } 477 1.1 elric } 478 1.1 elric if (options->flags & KRB5_GET_INIT_CREDS_OPT_ETYPE_LIST) { 479 1.1 elric if (ctx->etypes) 480 1.1 elric free(ctx->etypes); 481 1.1 elric 482 1.1 elric etypes = malloc((options->etype_list_length + 1) 483 1.1 elric * sizeof(krb5_enctype)); 484 1.1 elric if (etypes == NULL) { 485 1.2 christos ret = krb5_enomem(context); 486 1.1 elric goto out; 487 1.1 elric } 488 1.1 elric memcpy (etypes, options->etype_list, 489 1.1 elric options->etype_list_length * sizeof(krb5_enctype)); 490 1.1 elric etypes[options->etype_list_length] = ETYPE_NULL; 491 1.1 elric ctx->etypes = etypes; 492 1.1 elric } 493 1.1 elric if (options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST) { 494 1.1 elric pre_auth_types = malloc((options->preauth_list_length + 1) 495 1.1 elric * sizeof(krb5_preauthtype)); 496 1.1 elric if (pre_auth_types == NULL) { 497 1.2 christos ret = krb5_enomem(context); 498 1.1 elric goto out; 499 1.1 elric } 500 1.1 elric memcpy (pre_auth_types, options->preauth_list, 501 1.1 elric options->preauth_list_length * sizeof(krb5_preauthtype)); 502 1.1 elric pre_auth_types[options->preauth_list_length] = KRB5_PADATA_NONE; 503 1.1 elric ctx->pre_auth_types = pre_auth_types; 504 1.1 elric } 505 1.1 elric if (options->flags & KRB5_GET_INIT_CREDS_OPT_ANONYMOUS) 506 1.1 elric ctx->flags.request_anonymous = options->anonymous; 507 1.1 elric if (default_opt) 508 1.1 elric krb5_get_init_creds_opt_free(context, default_opt); 509 1.1 elric return 0; 510 1.1 elric out: 511 1.1 elric if (default_opt) 512 1.1 elric krb5_get_init_creds_opt_free(context, default_opt); 513 1.1 elric return ret; 514 1.1 elric } 515 1.1 elric 516 1.1 elric static krb5_error_code 517 1.1 elric change_password (krb5_context context, 518 1.1 elric krb5_principal client, 519 1.1 elric const char *password, 520 1.1 elric char *newpw, 521 1.1 elric size_t newpw_sz, 522 1.1 elric krb5_prompter_fct prompter, 523 1.1 elric void *data, 524 1.1 elric krb5_get_init_creds_opt *old_options) 525 1.1 elric { 526 1.1 elric krb5_prompt prompts[2]; 527 1.1 elric krb5_error_code ret; 528 1.1 elric krb5_creds cpw_cred; 529 1.1 elric char buf1[BUFSIZ], buf2[BUFSIZ]; 530 1.1 elric krb5_data password_data[2]; 531 1.1 elric int result_code; 532 1.1 elric krb5_data result_code_string; 533 1.1 elric krb5_data result_string; 534 1.1 elric char *p; 535 1.1 elric krb5_get_init_creds_opt *options; 536 1.1 elric 537 1.2 christos heim_assert(prompter != NULL, "unexpected NULL prompter"); 538 1.2 christos 539 1.1 elric memset (&cpw_cred, 0, sizeof(cpw_cred)); 540 1.1 elric 541 1.1 elric ret = krb5_get_init_creds_opt_alloc(context, &options); 542 1.1 elric if (ret) 543 1.1 elric return ret; 544 1.1 elric krb5_get_init_creds_opt_set_tkt_life (options, 60); 545 1.1 elric krb5_get_init_creds_opt_set_forwardable (options, FALSE); 546 1.1 elric krb5_get_init_creds_opt_set_proxiable (options, FALSE); 547 1.2 christos if (old_options && 548 1.2 christos (old_options->flags & KRB5_GET_INIT_CREDS_OPT_PREAUTH_LIST)) 549 1.2 christos krb5_get_init_creds_opt_set_preauth_list(options, 550 1.2 christos old_options->preauth_list, 551 1.2 christos old_options->preauth_list_length); 552 1.2 christos if (old_options && 553 1.2 christos (old_options->flags & KRB5_GET_INIT_CREDS_OPT_CHANGE_PASSWORD_PROMPT)) 554 1.2 christos krb5_get_init_creds_opt_set_change_password_prompt(options, 555 1.2 christos old_options->change_password_prompt); 556 1.1 elric 557 1.1 elric krb5_data_zero (&result_code_string); 558 1.1 elric krb5_data_zero (&result_string); 559 1.1 elric 560 1.1 elric ret = krb5_get_init_creds_password (context, 561 1.1 elric &cpw_cred, 562 1.1 elric client, 563 1.1 elric password, 564 1.1 elric prompter, 565 1.1 elric data, 566 1.1 elric 0, 567 1.1 elric "kadmin/changepw", 568 1.1 elric options); 569 1.1 elric krb5_get_init_creds_opt_free(context, options); 570 1.1 elric if (ret) 571 1.1 elric goto out; 572 1.1 elric 573 1.1 elric for(;;) { 574 1.1 elric password_data[0].data = buf1; 575 1.1 elric password_data[0].length = sizeof(buf1); 576 1.1 elric 577 1.1 elric prompts[0].hidden = 1; 578 1.1 elric prompts[0].prompt = "New password: "; 579 1.1 elric prompts[0].reply = &password_data[0]; 580 1.1 elric prompts[0].type = KRB5_PROMPT_TYPE_NEW_PASSWORD; 581 1.1 elric 582 1.1 elric password_data[1].data = buf2; 583 1.1 elric password_data[1].length = sizeof(buf2); 584 1.1 elric 585 1.1 elric prompts[1].hidden = 1; 586 1.1 elric prompts[1].prompt = "Repeat new password: "; 587 1.1 elric prompts[1].reply = &password_data[1]; 588 1.1 elric prompts[1].type = KRB5_PROMPT_TYPE_NEW_PASSWORD_AGAIN; 589 1.1 elric 590 1.1 elric ret = (*prompter) (context, data, NULL, "Changing password", 591 1.1 elric 2, prompts); 592 1.1 elric if (ret) { 593 1.1 elric memset (buf1, 0, sizeof(buf1)); 594 1.1 elric memset (buf2, 0, sizeof(buf2)); 595 1.1 elric goto out; 596 1.1 elric } 597 1.1 elric 598 1.1 elric if (strcmp (buf1, buf2) == 0) 599 1.1 elric break; 600 1.1 elric memset (buf1, 0, sizeof(buf1)); 601 1.1 elric memset (buf2, 0, sizeof(buf2)); 602 1.1 elric } 603 1.1 elric 604 1.1 elric ret = krb5_set_password (context, 605 1.1 elric &cpw_cred, 606 1.1 elric buf1, 607 1.1 elric client, 608 1.1 elric &result_code, 609 1.1 elric &result_code_string, 610 1.1 elric &result_string); 611 1.1 elric if (ret) 612 1.1 elric goto out; 613 1.1 elric if (asprintf(&p, "%s: %.*s\n", 614 1.1 elric result_code ? "Error" : "Success", 615 1.1 elric (int)result_string.length, 616 1.1 elric result_string.length > 0 ? (char*)result_string.data : "") < 0) 617 1.1 elric { 618 1.1 elric ret = ENOMEM; 619 1.1 elric goto out; 620 1.1 elric } 621 1.1 elric 622 1.1 elric /* return the result */ 623 1.1 elric (*prompter) (context, data, NULL, p, 0, NULL); 624 1.1 elric 625 1.1 elric free (p); 626 1.1 elric if (result_code == 0) { 627 1.1 elric strlcpy (newpw, buf1, newpw_sz); 628 1.1 elric ret = 0; 629 1.1 elric } else { 630 1.1 elric ret = ENOTTY; 631 1.1 elric krb5_set_error_message(context, ret, 632 1.1 elric N_("failed changing password", "")); 633 1.1 elric } 634 1.1 elric 635 1.1 elric out: 636 1.3 christos memset_s(buf1, sizeof(buf1), 0, sizeof(buf1)); 637 1.3 christos memset_s(buf2, sizeof(buf2), 0, sizeof(buf2)); 638 1.1 elric krb5_data_free (&result_string); 639 1.1 elric krb5_data_free (&result_code_string); 640 1.1 elric krb5_free_cred_contents (context, &cpw_cred); 641 1.1 elric return ret; 642 1.1 elric } 643 1.1 elric 644 1.1 elric 645 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 646 1.1 elric krb5_keyblock_key_proc (krb5_context context, 647 1.1 elric krb5_keytype type, 648 1.1 elric krb5_data *salt, 649 1.1 elric krb5_const_pointer keyseed, 650 1.1 elric krb5_keyblock **key) 651 1.1 elric { 652 1.1 elric return krb5_copy_keyblock (context, keyseed, key); 653 1.1 elric } 654 1.1 elric 655 1.1 elric /* 656 1.1 elric * 657 1.1 elric */ 658 1.1 elric 659 1.1 elric static krb5_error_code 660 1.1 elric init_as_req (krb5_context context, 661 1.1 elric KDCOptions opts, 662 1.1 elric const krb5_creds *creds, 663 1.1 elric const krb5_addresses *addrs, 664 1.1 elric const krb5_enctype *etypes, 665 1.1 elric AS_REQ *a) 666 1.1 elric { 667 1.1 elric krb5_error_code ret; 668 1.1 elric 669 1.1 elric memset(a, 0, sizeof(*a)); 670 1.1 elric 671 1.1 elric a->pvno = 5; 672 1.1 elric a->msg_type = krb_as_req; 673 1.1 elric a->req_body.kdc_options = opts; 674 1.1 elric a->req_body.cname = malloc(sizeof(*a->req_body.cname)); 675 1.1 elric if (a->req_body.cname == NULL) { 676 1.2 christos ret = krb5_enomem(context); 677 1.1 elric goto fail; 678 1.1 elric } 679 1.1 elric a->req_body.sname = malloc(sizeof(*a->req_body.sname)); 680 1.1 elric if (a->req_body.sname == NULL) { 681 1.2 christos ret = krb5_enomem(context); 682 1.1 elric goto fail; 683 1.1 elric } 684 1.1 elric 685 1.1 elric ret = _krb5_principal2principalname (a->req_body.cname, creds->client); 686 1.1 elric if (ret) 687 1.1 elric goto fail; 688 1.1 elric ret = copy_Realm(&creds->client->realm, &a->req_body.realm); 689 1.1 elric if (ret) 690 1.1 elric goto fail; 691 1.1 elric 692 1.1 elric ret = _krb5_principal2principalname (a->req_body.sname, creds->server); 693 1.1 elric if (ret) 694 1.1 elric goto fail; 695 1.1 elric 696 1.1 elric if(creds->times.starttime) { 697 1.1 elric a->req_body.from = malloc(sizeof(*a->req_body.from)); 698 1.1 elric if (a->req_body.from == NULL) { 699 1.2 christos ret = krb5_enomem(context); 700 1.1 elric goto fail; 701 1.1 elric } 702 1.1 elric *a->req_body.from = creds->times.starttime; 703 1.1 elric } 704 1.1 elric if(creds->times.endtime){ 705 1.2 christos if ((ALLOC(a->req_body.till, 1)) != NULL) 706 1.2 christos *a->req_body.till = creds->times.endtime; 707 1.2 christos else { 708 1.2 christos ret = krb5_enomem(context); 709 1.2 christos goto fail; 710 1.2 christos } 711 1.1 elric } 712 1.1 elric if(creds->times.renew_till){ 713 1.1 elric a->req_body.rtime = malloc(sizeof(*a->req_body.rtime)); 714 1.1 elric if (a->req_body.rtime == NULL) { 715 1.2 christos ret = krb5_enomem(context); 716 1.1 elric goto fail; 717 1.1 elric } 718 1.1 elric *a->req_body.rtime = creds->times.renew_till; 719 1.1 elric } 720 1.1 elric a->req_body.nonce = 0; 721 1.2 christos ret = _krb5_init_etype(context, 722 1.2 christos KRB5_PDU_AS_REQUEST, 723 1.1 elric &a->req_body.etype.len, 724 1.1 elric &a->req_body.etype.val, 725 1.1 elric etypes); 726 1.1 elric if (ret) 727 1.1 elric goto fail; 728 1.1 elric 729 1.1 elric /* 730 1.1 elric * This means no addresses 731 1.1 elric */ 732 1.1 elric 733 1.1 elric if (addrs && addrs->len == 0) { 734 1.1 elric a->req_body.addresses = NULL; 735 1.1 elric } else { 736 1.1 elric a->req_body.addresses = malloc(sizeof(*a->req_body.addresses)); 737 1.1 elric if (a->req_body.addresses == NULL) { 738 1.2 christos ret = krb5_enomem(context); 739 1.1 elric goto fail; 740 1.1 elric } 741 1.1 elric 742 1.1 elric if (addrs) 743 1.1 elric ret = krb5_copy_addresses(context, addrs, a->req_body.addresses); 744 1.1 elric else { 745 1.1 elric ret = krb5_get_all_client_addrs (context, a->req_body.addresses); 746 1.1 elric if(ret == 0 && a->req_body.addresses->len == 0) { 747 1.1 elric free(a->req_body.addresses); 748 1.1 elric a->req_body.addresses = NULL; 749 1.1 elric } 750 1.1 elric } 751 1.1 elric if (ret) 752 1.1 elric goto fail; 753 1.1 elric } 754 1.1 elric 755 1.1 elric a->req_body.enc_authorization_data = NULL; 756 1.1 elric a->req_body.additional_tickets = NULL; 757 1.1 elric 758 1.1 elric a->padata = NULL; 759 1.1 elric 760 1.1 elric return 0; 761 1.1 elric fail: 762 1.1 elric free_AS_REQ(a); 763 1.3 christos memset_s(a, sizeof(*a), 0, sizeof(*a)); 764 1.1 elric return ret; 765 1.1 elric } 766 1.1 elric 767 1.1 elric 768 1.1 elric static krb5_error_code 769 1.1 elric set_paid(struct pa_info_data *paid, krb5_context context, 770 1.1 elric krb5_enctype etype, 771 1.1 elric krb5_salttype salttype, void *salt_string, size_t salt_len, 772 1.1 elric krb5_data *s2kparams) 773 1.1 elric { 774 1.1 elric paid->etype = etype; 775 1.1 elric paid->salt.salttype = salttype; 776 1.1 elric paid->salt.saltvalue.data = malloc(salt_len + 1); 777 1.1 elric if (paid->salt.saltvalue.data == NULL) { 778 1.1 elric krb5_clear_error_message(context); 779 1.1 elric return ENOMEM; 780 1.1 elric } 781 1.1 elric memcpy(paid->salt.saltvalue.data, salt_string, salt_len); 782 1.1 elric ((char *)paid->salt.saltvalue.data)[salt_len] = '\0'; 783 1.1 elric paid->salt.saltvalue.length = salt_len; 784 1.1 elric if (s2kparams) { 785 1.1 elric krb5_error_code ret; 786 1.1 elric 787 1.1 elric ret = krb5_copy_data(context, s2kparams, &paid->s2kparams); 788 1.1 elric if (ret) { 789 1.1 elric krb5_clear_error_message(context); 790 1.1 elric krb5_free_salt(context, paid->salt); 791 1.1 elric return ret; 792 1.1 elric } 793 1.1 elric } else 794 1.1 elric paid->s2kparams = NULL; 795 1.1 elric 796 1.1 elric return 0; 797 1.1 elric } 798 1.1 elric 799 1.1 elric static struct pa_info_data * 800 1.1 elric pa_etype_info2(krb5_context context, 801 1.1 elric const krb5_principal client, 802 1.1 elric const AS_REQ *asreq, 803 1.1 elric struct pa_info_data *paid, 804 1.1 elric heim_octet_string *data) 805 1.1 elric { 806 1.1 elric krb5_error_code ret; 807 1.1 elric ETYPE_INFO2 e; 808 1.1 elric size_t sz; 809 1.2 christos size_t i, j; 810 1.1 elric 811 1.1 elric memset(&e, 0, sizeof(e)); 812 1.1 elric ret = decode_ETYPE_INFO2(data->data, data->length, &e, &sz); 813 1.1 elric if (ret) 814 1.1 elric goto out; 815 1.1 elric if (e.len == 0) 816 1.1 elric goto out; 817 1.1 elric for (j = 0; j < asreq->req_body.etype.len; j++) { 818 1.1 elric for (i = 0; i < e.len; i++) { 819 1.1 elric if (asreq->req_body.etype.val[j] == e.val[i].etype) { 820 1.1 elric krb5_salt salt; 821 1.1 elric if (e.val[i].salt == NULL) 822 1.1 elric ret = krb5_get_pw_salt(context, client, &salt); 823 1.1 elric else { 824 1.1 elric salt.saltvalue.data = *e.val[i].salt; 825 1.1 elric salt.saltvalue.length = strlen(*e.val[i].salt); 826 1.1 elric ret = 0; 827 1.1 elric } 828 1.1 elric if (ret == 0) 829 1.1 elric ret = set_paid(paid, context, e.val[i].etype, 830 1.1 elric KRB5_PW_SALT, 831 1.1 elric salt.saltvalue.data, 832 1.1 elric salt.saltvalue.length, 833 1.1 elric e.val[i].s2kparams); 834 1.1 elric if (e.val[i].salt == NULL) 835 1.1 elric krb5_free_salt(context, salt); 836 1.1 elric if (ret == 0) { 837 1.1 elric free_ETYPE_INFO2(&e); 838 1.1 elric return paid; 839 1.1 elric } 840 1.1 elric } 841 1.1 elric } 842 1.1 elric } 843 1.1 elric out: 844 1.1 elric free_ETYPE_INFO2(&e); 845 1.1 elric return NULL; 846 1.1 elric } 847 1.1 elric 848 1.1 elric static struct pa_info_data * 849 1.1 elric pa_etype_info(krb5_context context, 850 1.1 elric const krb5_principal client, 851 1.1 elric const AS_REQ *asreq, 852 1.1 elric struct pa_info_data *paid, 853 1.1 elric heim_octet_string *data) 854 1.1 elric { 855 1.1 elric krb5_error_code ret; 856 1.1 elric ETYPE_INFO e; 857 1.1 elric size_t sz; 858 1.2 christos size_t i, j; 859 1.1 elric 860 1.1 elric memset(&e, 0, sizeof(e)); 861 1.1 elric ret = decode_ETYPE_INFO(data->data, data->length, &e, &sz); 862 1.1 elric if (ret) 863 1.1 elric goto out; 864 1.1 elric if (e.len == 0) 865 1.1 elric goto out; 866 1.1 elric for (j = 0; j < asreq->req_body.etype.len; j++) { 867 1.1 elric for (i = 0; i < e.len; i++) { 868 1.1 elric if (asreq->req_body.etype.val[j] == e.val[i].etype) { 869 1.1 elric krb5_salt salt; 870 1.1 elric salt.salttype = KRB5_PW_SALT; 871 1.1 elric if (e.val[i].salt == NULL) 872 1.1 elric ret = krb5_get_pw_salt(context, client, &salt); 873 1.1 elric else { 874 1.1 elric salt.saltvalue = *e.val[i].salt; 875 1.1 elric ret = 0; 876 1.1 elric } 877 1.1 elric if (e.val[i].salttype) 878 1.1 elric salt.salttype = *e.val[i].salttype; 879 1.1 elric if (ret == 0) { 880 1.1 elric ret = set_paid(paid, context, e.val[i].etype, 881 1.1 elric salt.salttype, 882 1.1 elric salt.saltvalue.data, 883 1.1 elric salt.saltvalue.length, 884 1.1 elric NULL); 885 1.1 elric if (e.val[i].salt == NULL) 886 1.1 elric krb5_free_salt(context, salt); 887 1.1 elric } 888 1.1 elric if (ret == 0) { 889 1.1 elric free_ETYPE_INFO(&e); 890 1.1 elric return paid; 891 1.1 elric } 892 1.1 elric } 893 1.1 elric } 894 1.1 elric } 895 1.1 elric out: 896 1.1 elric free_ETYPE_INFO(&e); 897 1.1 elric return NULL; 898 1.1 elric } 899 1.1 elric 900 1.1 elric static struct pa_info_data * 901 1.1 elric pa_pw_or_afs3_salt(krb5_context context, 902 1.1 elric const krb5_principal client, 903 1.1 elric const AS_REQ *asreq, 904 1.1 elric struct pa_info_data *paid, 905 1.1 elric heim_octet_string *data) 906 1.1 elric { 907 1.1 elric krb5_error_code ret; 908 1.2 christos if (paid->etype == KRB5_ENCTYPE_NULL) 909 1.1 elric return NULL; 910 1.1 elric ret = set_paid(paid, context, 911 1.1 elric paid->etype, 912 1.1 elric paid->salt.salttype, 913 1.1 elric data->data, 914 1.1 elric data->length, 915 1.1 elric NULL); 916 1.1 elric if (ret) 917 1.1 elric return NULL; 918 1.1 elric return paid; 919 1.1 elric } 920 1.1 elric 921 1.1 elric 922 1.1 elric struct pa_info { 923 1.1 elric krb5_preauthtype type; 924 1.1 elric struct pa_info_data *(*salt_info)(krb5_context, 925 1.1 elric const krb5_principal, 926 1.1 elric const AS_REQ *, 927 1.1 elric struct pa_info_data *, 928 1.1 elric heim_octet_string *); 929 1.1 elric }; 930 1.1 elric 931 1.1 elric static struct pa_info pa_prefs[] = { 932 1.1 elric { KRB5_PADATA_ETYPE_INFO2, pa_etype_info2 }, 933 1.1 elric { KRB5_PADATA_ETYPE_INFO, pa_etype_info }, 934 1.1 elric { KRB5_PADATA_PW_SALT, pa_pw_or_afs3_salt }, 935 1.1 elric { KRB5_PADATA_AFS3_SALT, pa_pw_or_afs3_salt } 936 1.1 elric }; 937 1.1 elric 938 1.1 elric static PA_DATA * 939 1.2 christos find_pa_data(const METHOD_DATA *md, unsigned type) 940 1.1 elric { 941 1.2 christos size_t i; 942 1.1 elric if (md == NULL) 943 1.1 elric return NULL; 944 1.1 elric for (i = 0; i < md->len; i++) 945 1.1 elric if (md->val[i].padata_type == type) 946 1.1 elric return &md->val[i]; 947 1.1 elric return NULL; 948 1.1 elric } 949 1.1 elric 950 1.1 elric static struct pa_info_data * 951 1.1 elric process_pa_info(krb5_context context, 952 1.1 elric const krb5_principal client, 953 1.1 elric const AS_REQ *asreq, 954 1.1 elric struct pa_info_data *paid, 955 1.1 elric METHOD_DATA *md) 956 1.1 elric { 957 1.1 elric struct pa_info_data *p = NULL; 958 1.2 christos size_t i; 959 1.1 elric 960 1.1 elric for (i = 0; p == NULL && i < sizeof(pa_prefs)/sizeof(pa_prefs[0]); i++) { 961 1.1 elric PA_DATA *pa = find_pa_data(md, pa_prefs[i].type); 962 1.1 elric if (pa == NULL) 963 1.1 elric continue; 964 1.2 christos paid->salt.salttype = (krb5_salttype)pa_prefs[i].type; 965 1.1 elric p = (*pa_prefs[i].salt_info)(context, client, asreq, 966 1.1 elric paid, &pa->padata_value); 967 1.1 elric } 968 1.1 elric return p; 969 1.1 elric } 970 1.1 elric 971 1.1 elric static krb5_error_code 972 1.1 elric make_pa_enc_timestamp(krb5_context context, METHOD_DATA *md, 973 1.1 elric krb5_enctype etype, krb5_keyblock *key) 974 1.1 elric { 975 1.1 elric PA_ENC_TS_ENC p; 976 1.1 elric unsigned char *buf; 977 1.1 elric size_t buf_size; 978 1.2 christos size_t len = 0; 979 1.1 elric EncryptedData encdata; 980 1.1 elric krb5_error_code ret; 981 1.1 elric int32_t usec; 982 1.1 elric int usec2; 983 1.1 elric krb5_crypto crypto; 984 1.1 elric 985 1.1 elric krb5_us_timeofday (context, &p.patimestamp, &usec); 986 1.1 elric usec2 = usec; 987 1.1 elric p.pausec = &usec2; 988 1.1 elric 989 1.1 elric ASN1_MALLOC_ENCODE(PA_ENC_TS_ENC, buf, buf_size, &p, &len, ret); 990 1.1 elric if (ret) 991 1.1 elric return ret; 992 1.1 elric if(buf_size != len) 993 1.1 elric krb5_abortx(context, "internal error in ASN.1 encoder"); 994 1.1 elric 995 1.1 elric ret = krb5_crypto_init(context, key, 0, &crypto); 996 1.1 elric if (ret) { 997 1.1 elric free(buf); 998 1.1 elric return ret; 999 1.1 elric } 1000 1.1 elric ret = krb5_encrypt_EncryptedData(context, 1001 1.1 elric crypto, 1002 1.1 elric KRB5_KU_PA_ENC_TIMESTAMP, 1003 1.1 elric buf, 1004 1.1 elric len, 1005 1.1 elric 0, 1006 1.1 elric &encdata); 1007 1.1 elric free(buf); 1008 1.1 elric krb5_crypto_destroy(context, crypto); 1009 1.1 elric if (ret) 1010 1.1 elric return ret; 1011 1.1 elric 1012 1.1 elric ASN1_MALLOC_ENCODE(EncryptedData, buf, buf_size, &encdata, &len, ret); 1013 1.1 elric free_EncryptedData(&encdata); 1014 1.1 elric if (ret) 1015 1.1 elric return ret; 1016 1.1 elric if(buf_size != len) 1017 1.1 elric krb5_abortx(context, "internal error in ASN.1 encoder"); 1018 1.1 elric 1019 1.1 elric ret = krb5_padata_add(context, md, KRB5_PADATA_ENC_TIMESTAMP, buf, len); 1020 1.1 elric if (ret) 1021 1.1 elric free(buf); 1022 1.1 elric return ret; 1023 1.1 elric } 1024 1.1 elric 1025 1.1 elric static krb5_error_code 1026 1.1 elric add_enc_ts_padata(krb5_context context, 1027 1.1 elric METHOD_DATA *md, 1028 1.1 elric krb5_principal client, 1029 1.1 elric krb5_s2k_proc keyproc, 1030 1.1 elric krb5_const_pointer keyseed, 1031 1.1 elric krb5_enctype *enctypes, 1032 1.1 elric unsigned netypes, 1033 1.1 elric krb5_salt *salt, 1034 1.1 elric krb5_data *s2kparams) 1035 1.1 elric { 1036 1.1 elric krb5_error_code ret; 1037 1.1 elric krb5_salt salt2; 1038 1.1 elric krb5_enctype *ep; 1039 1.2 christos size_t i; 1040 1.1 elric 1041 1.1 elric if(salt == NULL) { 1042 1.1 elric /* default to standard salt */ 1043 1.1 elric ret = krb5_get_pw_salt (context, client, &salt2); 1044 1.1 elric if (ret) 1045 1.1 elric return ret; 1046 1.1 elric salt = &salt2; 1047 1.1 elric } 1048 1.1 elric if (!enctypes) { 1049 1.1 elric enctypes = context->etypes; 1050 1.1 elric netypes = 0; 1051 1.2 christos for (ep = enctypes; *ep != (krb5_enctype)ETYPE_NULL; ep++) 1052 1.1 elric netypes++; 1053 1.1 elric } 1054 1.1 elric 1055 1.1 elric for (i = 0; i < netypes; ++i) { 1056 1.1 elric krb5_keyblock *key; 1057 1.1 elric 1058 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: using ENC-TS with enctype %d", enctypes[i]); 1059 1.1 elric 1060 1.1 elric ret = (*keyproc)(context, enctypes[i], keyseed, 1061 1.1 elric *salt, s2kparams, &key); 1062 1.1 elric if (ret) 1063 1.1 elric continue; 1064 1.1 elric ret = make_pa_enc_timestamp (context, md, enctypes[i], key); 1065 1.1 elric krb5_free_keyblock (context, key); 1066 1.1 elric if (ret) 1067 1.1 elric return ret; 1068 1.1 elric } 1069 1.1 elric if(salt == &salt2) 1070 1.1 elric krb5_free_salt(context, salt2); 1071 1.1 elric return 0; 1072 1.1 elric } 1073 1.1 elric 1074 1.1 elric static krb5_error_code 1075 1.1 elric pa_data_to_md_ts_enc(krb5_context context, 1076 1.1 elric const AS_REQ *a, 1077 1.1 elric const krb5_principal client, 1078 1.1 elric krb5_get_init_creds_ctx *ctx, 1079 1.1 elric struct pa_info_data *ppaid, 1080 1.1 elric METHOD_DATA *md) 1081 1.1 elric { 1082 1.1 elric if (ctx->keyproc == NULL || ctx->keyseed == NULL) 1083 1.1 elric return 0; 1084 1.1 elric 1085 1.1 elric if (ppaid) { 1086 1.1 elric add_enc_ts_padata(context, md, client, 1087 1.1 elric ctx->keyproc, ctx->keyseed, 1088 1.1 elric &ppaid->etype, 1, 1089 1.1 elric &ppaid->salt, ppaid->s2kparams); 1090 1.1 elric } else { 1091 1.1 elric krb5_salt salt; 1092 1.1 elric 1093 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: pa-info not found, guessing salt"); 1094 1.1 elric 1095 1.1 elric /* make a v5 salted pa-data */ 1096 1.1 elric add_enc_ts_padata(context, md, client, 1097 1.1 elric ctx->keyproc, ctx->keyseed, 1098 1.1 elric a->req_body.etype.val, a->req_body.etype.len, 1099 1.1 elric NULL, NULL); 1100 1.1 elric 1101 1.1 elric /* make a v4 salted pa-data */ 1102 1.1 elric salt.salttype = KRB5_PW_SALT; 1103 1.1 elric krb5_data_zero(&salt.saltvalue); 1104 1.1 elric add_enc_ts_padata(context, md, client, 1105 1.1 elric ctx->keyproc, ctx->keyseed, 1106 1.1 elric a->req_body.etype.val, a->req_body.etype.len, 1107 1.1 elric &salt, NULL); 1108 1.1 elric } 1109 1.1 elric return 0; 1110 1.1 elric } 1111 1.1 elric 1112 1.1 elric static krb5_error_code 1113 1.1 elric pa_data_to_key_plain(krb5_context context, 1114 1.1 elric const krb5_principal client, 1115 1.1 elric krb5_get_init_creds_ctx *ctx, 1116 1.1 elric krb5_salt salt, 1117 1.1 elric krb5_data *s2kparams, 1118 1.1 elric krb5_enctype etype, 1119 1.1 elric krb5_keyblock **key) 1120 1.1 elric { 1121 1.1 elric krb5_error_code ret; 1122 1.1 elric 1123 1.1 elric ret = (*ctx->keyproc)(context, etype, ctx->keyseed, 1124 1.1 elric salt, s2kparams, key); 1125 1.1 elric return ret; 1126 1.1 elric } 1127 1.1 elric 1128 1.1 elric 1129 1.1 elric static krb5_error_code 1130 1.1 elric pa_data_to_md_pkinit(krb5_context context, 1131 1.1 elric const AS_REQ *a, 1132 1.1 elric const krb5_principal client, 1133 1.1 elric int win2k, 1134 1.1 elric krb5_get_init_creds_ctx *ctx, 1135 1.1 elric METHOD_DATA *md) 1136 1.1 elric { 1137 1.1 elric if (ctx->pk_init_ctx == NULL) 1138 1.1 elric return 0; 1139 1.1 elric #ifdef PKINIT 1140 1.1 elric return _krb5_pk_mk_padata(context, 1141 1.1 elric ctx->pk_init_ctx, 1142 1.1 elric ctx->ic_flags, 1143 1.1 elric win2k, 1144 1.1 elric &a->req_body, 1145 1.1 elric ctx->pk_nonce, 1146 1.1 elric md); 1147 1.1 elric #else 1148 1.1 elric krb5_set_error_message(context, EINVAL, 1149 1.1 elric N_("no support for PKINIT compiled in", "")); 1150 1.1 elric return EINVAL; 1151 1.1 elric #endif 1152 1.1 elric } 1153 1.1 elric 1154 1.1 elric static krb5_error_code 1155 1.1 elric pa_data_add_pac_request(krb5_context context, 1156 1.1 elric krb5_get_init_creds_ctx *ctx, 1157 1.1 elric METHOD_DATA *md) 1158 1.1 elric { 1159 1.2 christos size_t len = 0, length; 1160 1.1 elric krb5_error_code ret; 1161 1.1 elric PA_PAC_REQUEST req; 1162 1.1 elric void *buf; 1163 1.1 elric 1164 1.1 elric switch (ctx->req_pac) { 1165 1.1 elric case KRB5_INIT_CREDS_TRISTATE_UNSET: 1166 1.1 elric return 0; /* don't bother */ 1167 1.1 elric case KRB5_INIT_CREDS_TRISTATE_TRUE: 1168 1.1 elric req.include_pac = 1; 1169 1.1 elric break; 1170 1.1 elric case KRB5_INIT_CREDS_TRISTATE_FALSE: 1171 1.1 elric req.include_pac = 0; 1172 1.1 elric } 1173 1.1 elric 1174 1.1 elric ASN1_MALLOC_ENCODE(PA_PAC_REQUEST, buf, length, 1175 1.1 elric &req, &len, ret); 1176 1.1 elric if (ret) 1177 1.1 elric return ret; 1178 1.1 elric if(len != length) 1179 1.1 elric krb5_abortx(context, "internal error in ASN.1 encoder"); 1180 1.1 elric 1181 1.1 elric ret = krb5_padata_add(context, md, KRB5_PADATA_PA_PAC_REQUEST, buf, len); 1182 1.1 elric if (ret) 1183 1.1 elric free(buf); 1184 1.1 elric 1185 1.1 elric return 0; 1186 1.1 elric } 1187 1.1 elric 1188 1.1 elric /* 1189 1.1 elric * Assumes caller always will free `out_md', even on error. 1190 1.1 elric */ 1191 1.1 elric 1192 1.1 elric static krb5_error_code 1193 1.1 elric process_pa_data_to_md(krb5_context context, 1194 1.1 elric const krb5_creds *creds, 1195 1.1 elric const AS_REQ *a, 1196 1.1 elric krb5_get_init_creds_ctx *ctx, 1197 1.1 elric METHOD_DATA *in_md, 1198 1.1 elric METHOD_DATA **out_md, 1199 1.1 elric krb5_prompter_fct prompter, 1200 1.1 elric void *prompter_data) 1201 1.1 elric { 1202 1.1 elric krb5_error_code ret; 1203 1.1 elric 1204 1.1 elric ALLOC(*out_md, 1); 1205 1.2 christos if (*out_md == NULL) 1206 1.2 christos return krb5_enomem(context); 1207 1.2 christos 1208 1.1 elric (*out_md)->len = 0; 1209 1.1 elric (*out_md)->val = NULL; 1210 1.1 elric 1211 1.1 elric if (_krb5_have_debug(context, 5)) { 1212 1.1 elric unsigned i; 1213 1.1 elric _krb5_debug(context, 5, "KDC send %d patypes", in_md->len); 1214 1.1 elric for (i = 0; i < in_md->len; i++) 1215 1.1 elric _krb5_debug(context, 5, "KDC send PA-DATA type: %d", in_md->val[i].padata_type); 1216 1.1 elric } 1217 1.1 elric 1218 1.1 elric /* 1219 1.1 elric * Make sure we don't sent both ENC-TS and PK-INIT pa data, no 1220 1.1 elric * need to expose our password protecting our PKCS12 key. 1221 1.1 elric */ 1222 1.1 elric 1223 1.1 elric if (ctx->pk_init_ctx) { 1224 1.1 elric 1225 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: " 1226 1.1 elric "prepareing PKINIT padata (%s)", 1227 1.1 elric (ctx->used_pa_types & USED_PKINIT_W2K) ? "win2k" : "ietf"); 1228 1.2 christos 1229 1.1 elric if (ctx->used_pa_types & USED_PKINIT_W2K) { 1230 1.1 elric krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP, 1231 1.1 elric "Already tried pkinit, looping"); 1232 1.1 elric return KRB5_GET_IN_TKT_LOOP; 1233 1.1 elric } 1234 1.1 elric 1235 1.2 christos ret = pa_data_to_md_pkinit(context, a, creds->client, 1236 1.1 elric (ctx->used_pa_types & USED_PKINIT), 1237 1.1 elric ctx, *out_md); 1238 1.1 elric if (ret) 1239 1.1 elric return ret; 1240 1.1 elric 1241 1.1 elric if (ctx->used_pa_types & USED_PKINIT) 1242 1.1 elric ctx->used_pa_types |= USED_PKINIT_W2K; 1243 1.1 elric else 1244 1.1 elric ctx->used_pa_types |= USED_PKINIT; 1245 1.1 elric 1246 1.1 elric } else if (in_md->len != 0) { 1247 1.1 elric struct pa_info_data *paid, *ppaid; 1248 1.1 elric unsigned flag; 1249 1.1 elric 1250 1.1 elric paid = calloc(1, sizeof(*paid)); 1251 1.2 christos if (paid == NULL) 1252 1.2 christos return krb5_enomem(context); 1253 1.1 elric 1254 1.2 christos paid->etype = KRB5_ENCTYPE_NULL; 1255 1.1 elric ppaid = process_pa_info(context, creds->client, a, paid, in_md); 1256 1.1 elric 1257 1.1 elric if (ppaid) 1258 1.1 elric flag = USED_ENC_TS_INFO; 1259 1.1 elric else 1260 1.1 elric flag = USED_ENC_TS_GUESS; 1261 1.1 elric 1262 1.1 elric if (ctx->used_pa_types & flag) { 1263 1.1 elric if (ppaid) 1264 1.1 elric free_paid(context, ppaid); 1265 1.2 christos free(paid); 1266 1.1 elric krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP, 1267 1.1 elric "Already tried ENC-TS-%s, looping", 1268 1.1 elric flag == USED_ENC_TS_INFO ? "info" : "guess"); 1269 1.1 elric return KRB5_GET_IN_TKT_LOOP; 1270 1.1 elric } 1271 1.1 elric 1272 1.1 elric pa_data_to_md_ts_enc(context, a, creds->client, ctx, ppaid, *out_md); 1273 1.1 elric 1274 1.1 elric ctx->used_pa_types |= flag; 1275 1.1 elric 1276 1.1 elric if (ppaid) { 1277 1.1 elric if (ctx->ppaid) { 1278 1.1 elric free_paid(context, ctx->ppaid); 1279 1.1 elric free(ctx->ppaid); 1280 1.1 elric } 1281 1.1 elric ctx->ppaid = ppaid; 1282 1.1 elric } else 1283 1.1 elric free(paid); 1284 1.1 elric } 1285 1.1 elric 1286 1.1 elric pa_data_add_pac_request(context, ctx, *out_md); 1287 1.1 elric 1288 1.2 christos if ((ctx->fast_state.flags & KRB5_FAST_DISABLED) == 0) { 1289 1.2 christos ret = krb5_padata_add(context, *out_md, KRB5_PADATA_REQ_ENC_PA_REP, NULL, 0); 1290 1.2 christos if (ret) 1291 1.2 christos return ret; 1292 1.2 christos } 1293 1.2 christos 1294 1.1 elric if ((*out_md)->len == 0) { 1295 1.1 elric free(*out_md); 1296 1.1 elric *out_md = NULL; 1297 1.1 elric } 1298 1.1 elric 1299 1.1 elric return 0; 1300 1.1 elric } 1301 1.1 elric 1302 1.1 elric static krb5_error_code 1303 1.1 elric process_pa_data_to_key(krb5_context context, 1304 1.1 elric krb5_get_init_creds_ctx *ctx, 1305 1.1 elric krb5_creds *creds, 1306 1.1 elric AS_REQ *a, 1307 1.1 elric AS_REP *rep, 1308 1.1 elric const krb5_krbhst_info *hi, 1309 1.1 elric krb5_keyblock **key) 1310 1.1 elric { 1311 1.1 elric struct pa_info_data paid, *ppaid = NULL; 1312 1.1 elric krb5_error_code ret; 1313 1.1 elric krb5_enctype etype; 1314 1.1 elric PA_DATA *pa; 1315 1.1 elric 1316 1.1 elric memset(&paid, 0, sizeof(paid)); 1317 1.1 elric 1318 1.1 elric etype = rep->enc_part.etype; 1319 1.1 elric 1320 1.1 elric if (rep->padata) { 1321 1.1 elric paid.etype = etype; 1322 1.1 elric ppaid = process_pa_info(context, creds->client, a, &paid, 1323 1.1 elric rep->padata); 1324 1.1 elric } 1325 1.1 elric if (ppaid == NULL) 1326 1.1 elric ppaid = ctx->ppaid; 1327 1.1 elric if (ppaid == NULL) { 1328 1.1 elric ret = krb5_get_pw_salt (context, creds->client, &paid.salt); 1329 1.1 elric if (ret) 1330 1.1 elric return ret; 1331 1.1 elric paid.etype = etype; 1332 1.1 elric paid.s2kparams = NULL; 1333 1.1 elric ppaid = &paid; 1334 1.1 elric } 1335 1.1 elric 1336 1.1 elric pa = NULL; 1337 1.1 elric if (rep->padata) { 1338 1.1 elric int idx = 0; 1339 1.1 elric pa = krb5_find_padata(rep->padata->val, 1340 1.1 elric rep->padata->len, 1341 1.1 elric KRB5_PADATA_PK_AS_REP, 1342 1.1 elric &idx); 1343 1.1 elric if (pa == NULL) { 1344 1.1 elric idx = 0; 1345 1.1 elric pa = krb5_find_padata(rep->padata->val, 1346 1.1 elric rep->padata->len, 1347 1.1 elric KRB5_PADATA_PK_AS_REP_19, 1348 1.1 elric &idx); 1349 1.1 elric } 1350 1.1 elric } 1351 1.1 elric if (pa && ctx->pk_init_ctx) { 1352 1.1 elric #ifdef PKINIT 1353 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: using PKINIT"); 1354 1.1 elric 1355 1.1 elric ret = _krb5_pk_rd_pa_reply(context, 1356 1.1 elric a->req_body.realm, 1357 1.1 elric ctx->pk_init_ctx, 1358 1.1 elric etype, 1359 1.1 elric hi, 1360 1.1 elric ctx->pk_nonce, 1361 1.1 elric &ctx->req_buffer, 1362 1.1 elric pa, 1363 1.1 elric key); 1364 1.1 elric #else 1365 1.1 elric ret = EINVAL; 1366 1.1 elric krb5_set_error_message(context, ret, N_("no support for PKINIT compiled in", "")); 1367 1.1 elric #endif 1368 1.1 elric } else if (ctx->keyseed) { 1369 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: using keyproc"); 1370 1.1 elric ret = pa_data_to_key_plain(context, creds->client, ctx, 1371 1.1 elric ppaid->salt, ppaid->s2kparams, etype, key); 1372 1.1 elric } else { 1373 1.1 elric ret = EINVAL; 1374 1.1 elric krb5_set_error_message(context, ret, N_("No usable pa data type", "")); 1375 1.1 elric } 1376 1.1 elric 1377 1.1 elric free_paid(context, &paid); 1378 1.1 elric return ret; 1379 1.1 elric } 1380 1.1 elric 1381 1.1 elric /** 1382 1.1 elric * Start a new context to get a new initial credential. 1383 1.1 elric * 1384 1.1 elric * @param context A Kerberos 5 context. 1385 1.1 elric * @param client The Kerberos principal to get the credential for, if 1386 1.1 elric * NULL is given, the default principal is used as determined by 1387 1.1 elric * krb5_get_default_principal(). 1388 1.1 elric * @param prompter 1389 1.1 elric * @param prompter_data 1390 1.1 elric * @param start_time the time the ticket should start to be valid or 0 for now. 1391 1.1 elric * @param options a options structure, can be NULL for default options. 1392 1.1 elric * @param rctx A new allocated free with krb5_init_creds_free(). 1393 1.1 elric * 1394 1.1 elric * @return 0 for success or an Kerberos 5 error code, see krb5_get_error_message(). 1395 1.1 elric * 1396 1.1 elric * @ingroup krb5_credential 1397 1.1 elric */ 1398 1.1 elric 1399 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 1400 1.1 elric krb5_init_creds_init(krb5_context context, 1401 1.1 elric krb5_principal client, 1402 1.1 elric krb5_prompter_fct prompter, 1403 1.1 elric void *prompter_data, 1404 1.1 elric krb5_deltat start_time, 1405 1.1 elric krb5_get_init_creds_opt *options, 1406 1.1 elric krb5_init_creds_context *rctx) 1407 1.1 elric { 1408 1.1 elric krb5_init_creds_context ctx; 1409 1.1 elric krb5_error_code ret; 1410 1.1 elric 1411 1.1 elric *rctx = NULL; 1412 1.1 elric 1413 1.1 elric ctx = calloc(1, sizeof(*ctx)); 1414 1.2 christos if (ctx == NULL) 1415 1.2 christos return krb5_enomem(context); 1416 1.1 elric 1417 1.1 elric ret = get_init_creds_common(context, client, start_time, options, ctx); 1418 1.1 elric if (ret) { 1419 1.1 elric free(ctx); 1420 1.1 elric return ret; 1421 1.1 elric } 1422 1.1 elric 1423 1.1 elric /* Set a new nonce. */ 1424 1.1 elric krb5_generate_random_block (&ctx->nonce, sizeof(ctx->nonce)); 1425 1.1 elric ctx->nonce &= 0x7fffffff; 1426 1.1 elric /* XXX these just needs to be the same when using Windows PK-INIT */ 1427 1.1 elric ctx->pk_nonce = ctx->nonce; 1428 1.1 elric 1429 1.1 elric ctx->prompter = prompter; 1430 1.1 elric ctx->prompter_data = prompter_data; 1431 1.1 elric 1432 1.1 elric *rctx = ctx; 1433 1.1 elric 1434 1.1 elric return ret; 1435 1.1 elric } 1436 1.1 elric 1437 1.1 elric /** 1438 1.1 elric * Sets the service that the is requested. This call is only neede for 1439 1.1 elric * special initial tickets, by default the a krbtgt is fetched in the default realm. 1440 1.1 elric * 1441 1.1 elric * @param context a Kerberos 5 context. 1442 1.1 elric * @param ctx a krb5_init_creds_context context. 1443 1.1 elric * @param service the service given as a string, for example 1444 1.1 elric * "kadmind/admin". If NULL, the default krbtgt in the clients 1445 1.1 elric * realm is set. 1446 1.1 elric * 1447 1.1 elric * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message(). 1448 1.1 elric * @ingroup krb5_credential 1449 1.1 elric */ 1450 1.1 elric 1451 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 1452 1.1 elric krb5_init_creds_set_service(krb5_context context, 1453 1.1 elric krb5_init_creds_context ctx, 1454 1.1 elric const char *service) 1455 1.1 elric { 1456 1.1 elric krb5_const_realm client_realm; 1457 1.1 elric krb5_principal principal; 1458 1.1 elric krb5_error_code ret; 1459 1.1 elric 1460 1.1 elric client_realm = krb5_principal_get_realm (context, ctx->cred.client); 1461 1.1 elric 1462 1.1 elric if (service) { 1463 1.1 elric ret = krb5_parse_name (context, service, &principal); 1464 1.1 elric if (ret) 1465 1.1 elric return ret; 1466 1.1 elric krb5_principal_set_realm (context, principal, client_realm); 1467 1.1 elric } else { 1468 1.1 elric ret = krb5_make_principal(context, &principal, 1469 1.1 elric client_realm, KRB5_TGS_NAME, client_realm, 1470 1.1 elric NULL); 1471 1.1 elric if (ret) 1472 1.1 elric return ret; 1473 1.1 elric } 1474 1.1 elric 1475 1.1 elric /* 1476 1.1 elric * This is for Windows RODC that are picky about what name type 1477 1.1 elric * the server principal have, and the really strange part is that 1478 1.1 elric * they are picky about the AS-REQ name type and not the TGS-REQ 1479 1.1 elric * later. Oh well. 1480 1.1 elric */ 1481 1.1 elric 1482 1.1 elric if (krb5_principal_is_krbtgt(context, principal)) 1483 1.1 elric krb5_principal_set_type(context, principal, KRB5_NT_SRV_INST); 1484 1.1 elric 1485 1.1 elric krb5_free_principal(context, ctx->cred.server); 1486 1.1 elric ctx->cred.server = principal; 1487 1.1 elric 1488 1.1 elric return 0; 1489 1.1 elric } 1490 1.1 elric 1491 1.1 elric /** 1492 1.1 elric * Sets the password that will use for the request. 1493 1.1 elric * 1494 1.1 elric * @param context a Kerberos 5 context. 1495 1.1 elric * @param ctx ctx krb5_init_creds_context context. 1496 1.1 elric * @param password the password to use. 1497 1.1 elric * 1498 1.1 elric * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message(). 1499 1.1 elric * @ingroup krb5_credential 1500 1.1 elric */ 1501 1.1 elric 1502 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 1503 1.1 elric krb5_init_creds_set_password(krb5_context context, 1504 1.1 elric krb5_init_creds_context ctx, 1505 1.1 elric const char *password) 1506 1.1 elric { 1507 1.1 elric if (ctx->password) { 1508 1.3 christos size_t len; 1509 1.3 christos len = strlen(ctx->password); 1510 1.3 christos memset_s(ctx->password, len, 0, len); 1511 1.1 elric free(ctx->password); 1512 1.1 elric } 1513 1.1 elric if (password) { 1514 1.1 elric ctx->password = strdup(password); 1515 1.2 christos if (ctx->password == NULL) 1516 1.2 christos return krb5_enomem(context); 1517 1.1 elric ctx->keyseed = (void *) ctx->password; 1518 1.1 elric } else { 1519 1.1 elric ctx->keyseed = NULL; 1520 1.1 elric ctx->password = NULL; 1521 1.1 elric } 1522 1.1 elric 1523 1.1 elric return 0; 1524 1.1 elric } 1525 1.1 elric 1526 1.1 elric static krb5_error_code KRB5_CALLCONV 1527 1.1 elric keytab_key_proc(krb5_context context, krb5_enctype enctype, 1528 1.1 elric krb5_const_pointer keyseed, 1529 1.1 elric krb5_salt salt, krb5_data *s2kparms, 1530 1.1 elric krb5_keyblock **key) 1531 1.1 elric { 1532 1.1 elric krb5_keytab_key_proc_args *args = rk_UNCONST(keyseed); 1533 1.1 elric krb5_keytab keytab = args->keytab; 1534 1.1 elric krb5_principal principal = args->principal; 1535 1.1 elric krb5_error_code ret; 1536 1.1 elric krb5_keytab real_keytab; 1537 1.1 elric krb5_keytab_entry entry; 1538 1.1 elric 1539 1.1 elric if(keytab == NULL) 1540 1.1 elric krb5_kt_default(context, &real_keytab); 1541 1.1 elric else 1542 1.1 elric real_keytab = keytab; 1543 1.1 elric 1544 1.1 elric ret = krb5_kt_get_entry (context, real_keytab, principal, 1545 1.1 elric 0, enctype, &entry); 1546 1.4 christos if (ret == 0) { 1547 1.4 christos ret = krb5_copy_keyblock(context, &entry.keyblock, key); 1548 1.4 christos krb5_kt_free_entry(context, &entry); 1549 1.4 christos } 1550 1.1 elric 1551 1.1 elric if (keytab == NULL) 1552 1.1 elric krb5_kt_close (context, real_keytab); 1553 1.1 elric return ret; 1554 1.1 elric } 1555 1.1 elric 1556 1.1 elric 1557 1.1 elric /** 1558 1.1 elric * Set the keytab to use for authentication. 1559 1.1 elric * 1560 1.1 elric * @param context a Kerberos 5 context. 1561 1.1 elric * @param ctx ctx krb5_init_creds_context context. 1562 1.1 elric * @param keytab the keytab to read the key from. 1563 1.1 elric * 1564 1.1 elric * @return 0 for success, or an Kerberos 5 error code, see krb5_get_error_message(). 1565 1.1 elric * @ingroup krb5_credential 1566 1.1 elric */ 1567 1.1 elric 1568 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 1569 1.1 elric krb5_init_creds_set_keytab(krb5_context context, 1570 1.1 elric krb5_init_creds_context ctx, 1571 1.1 elric krb5_keytab keytab) 1572 1.1 elric { 1573 1.1 elric krb5_keytab_key_proc_args *a; 1574 1.1 elric krb5_keytab_entry entry; 1575 1.1 elric krb5_kt_cursor cursor; 1576 1.1 elric krb5_enctype *etypes = NULL; 1577 1.1 elric krb5_error_code ret; 1578 1.1 elric size_t netypes = 0; 1579 1.2 christos int kvno = 0, found = 0; 1580 1.2 christos 1581 1.1 elric a = malloc(sizeof(*a)); 1582 1.2 christos if (a == NULL) 1583 1.2 christos return krb5_enomem(context); 1584 1.2 christos 1585 1.1 elric a->principal = ctx->cred.client; 1586 1.1 elric a->keytab = keytab; 1587 1.1 elric 1588 1.1 elric ctx->keytab_data = a; 1589 1.1 elric ctx->keyseed = (void *)a; 1590 1.1 elric ctx->keyproc = keytab_key_proc; 1591 1.1 elric 1592 1.1 elric /* 1593 1.1 elric * We need to the KDC what enctypes we support for this keytab, 1594 1.1 elric * esp if the keytab is really a password based entry, then the 1595 1.1 elric * KDC might have more enctypes in the database then what we have 1596 1.1 elric * in the keytab. 1597 1.1 elric */ 1598 1.1 elric 1599 1.1 elric ret = krb5_kt_start_seq_get(context, keytab, &cursor); 1600 1.1 elric if(ret) 1601 1.1 elric goto out; 1602 1.1 elric 1603 1.1 elric while(krb5_kt_next_entry(context, keytab, &entry, &cursor) == 0){ 1604 1.1 elric void *ptr; 1605 1.1 elric 1606 1.1 elric if (!krb5_principal_compare(context, entry.principal, ctx->cred.client)) 1607 1.1 elric goto next; 1608 1.1 elric 1609 1.2 christos found = 1; 1610 1.2 christos 1611 1.1 elric /* check if we ahve this kvno already */ 1612 1.1 elric if (entry.vno > kvno) { 1613 1.1 elric /* remove old list of etype */ 1614 1.1 elric if (etypes) 1615 1.1 elric free(etypes); 1616 1.1 elric etypes = NULL; 1617 1.1 elric netypes = 0; 1618 1.1 elric kvno = entry.vno; 1619 1.1 elric } else if (entry.vno != kvno) 1620 1.1 elric goto next; 1621 1.2 christos 1622 1.1 elric /* check if enctype is supported */ 1623 1.1 elric if (krb5_enctype_valid(context, entry.keyblock.keytype) != 0) 1624 1.1 elric goto next; 1625 1.1 elric 1626 1.1 elric /* add enctype to supported list */ 1627 1.1 elric ptr = realloc(etypes, sizeof(etypes[0]) * (netypes + 2)); 1628 1.2 christos if (ptr == NULL) { 1629 1.2 christos free(etypes); 1630 1.2 christos ret = krb5_enomem(context); 1631 1.2 christos goto out; 1632 1.2 christos } 1633 1.1 elric 1634 1.1 elric etypes = ptr; 1635 1.1 elric etypes[netypes] = entry.keyblock.keytype; 1636 1.1 elric etypes[netypes + 1] = ETYPE_NULL; 1637 1.1 elric netypes++; 1638 1.1 elric next: 1639 1.1 elric krb5_kt_free_entry(context, &entry); 1640 1.1 elric } 1641 1.1 elric krb5_kt_end_seq_get(context, keytab, &cursor); 1642 1.1 elric 1643 1.1 elric if (etypes) { 1644 1.1 elric if (ctx->etypes) 1645 1.1 elric free(ctx->etypes); 1646 1.1 elric ctx->etypes = etypes; 1647 1.1 elric } 1648 1.1 elric 1649 1.1 elric out: 1650 1.2 christos if (!found) { 1651 1.2 christos if (ret == 0) 1652 1.2 christos ret = KRB5_KT_NOTFOUND; 1653 1.2 christos _krb5_kt_principal_not_found(context, ret, keytab, ctx->cred.client, 0, 0); 1654 1.2 christos } 1655 1.2 christos 1656 1.2 christos return ret; 1657 1.1 elric } 1658 1.1 elric 1659 1.1 elric static krb5_error_code KRB5_CALLCONV 1660 1.1 elric keyblock_key_proc(krb5_context context, krb5_enctype enctype, 1661 1.1 elric krb5_const_pointer keyseed, 1662 1.1 elric krb5_salt salt, krb5_data *s2kparms, 1663 1.1 elric krb5_keyblock **key) 1664 1.1 elric { 1665 1.1 elric return krb5_copy_keyblock (context, keyseed, key); 1666 1.1 elric } 1667 1.1 elric 1668 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 1669 1.1 elric krb5_init_creds_set_keyblock(krb5_context context, 1670 1.1 elric krb5_init_creds_context ctx, 1671 1.1 elric krb5_keyblock *keyblock) 1672 1.1 elric { 1673 1.1 elric ctx->keyseed = (void *)keyblock; 1674 1.1 elric ctx->keyproc = keyblock_key_proc; 1675 1.1 elric 1676 1.1 elric return 0; 1677 1.1 elric } 1678 1.1 elric 1679 1.2 christos KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 1680 1.2 christos krb5_init_creds_set_fast_ccache(krb5_context context, 1681 1.2 christos krb5_init_creds_context ctx, 1682 1.2 christos krb5_ccache fast_ccache) 1683 1.2 christos { 1684 1.2 christos ctx->fast_state.armor_ccache = fast_ccache; 1685 1.2 christos ctx->fast_state.flags |= KRB5_FAST_REQUIRED; 1686 1.2 christos return 0; 1687 1.2 christos } 1688 1.2 christos 1689 1.2 christos KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 1690 1.2 christos krb5_init_creds_set_fast_ap_armor_service(krb5_context context, 1691 1.2 christos krb5_init_creds_context ctx, 1692 1.2 christos krb5_const_principal armor_service) 1693 1.2 christos { 1694 1.2 christos krb5_error_code ret; 1695 1.2 christos 1696 1.2 christos if (ctx->fast_state.armor_service) 1697 1.2 christos krb5_free_principal(context, ctx->fast_state.armor_service); 1698 1.2 christos if (armor_service) { 1699 1.2 christos ret = krb5_copy_principal(context, armor_service, &ctx->fast_state.armor_service); 1700 1.2 christos if (ret) 1701 1.2 christos return ret; 1702 1.2 christos } else { 1703 1.2 christos ctx->fast_state.armor_service = NULL; 1704 1.2 christos } 1705 1.2 christos ctx->fast_state.flags |= KRB5_FAST_REQUIRED | KRB5_FAST_AP_ARMOR_SERVICE; 1706 1.2 christos return 0; 1707 1.2 christos } 1708 1.2 christos 1709 1.2 christos /* 1710 1.2 christos * FAST 1711 1.2 christos */ 1712 1.2 christos 1713 1.2 christos static krb5_error_code 1714 1.2 christos check_fast(krb5_context context, struct fast_state *state) 1715 1.2 christos { 1716 1.2 christos if (state->flags & KRB5_FAST_EXPECTED) { 1717 1.2 christos krb5_set_error_message(context, KRB5KRB_AP_ERR_MODIFIED, 1718 1.2 christos "Expected FAST, but no FAST " 1719 1.2 christos "was in the response from the KDC"); 1720 1.2 christos return KRB5KRB_AP_ERR_MODIFIED; 1721 1.2 christos } 1722 1.2 christos return 0; 1723 1.2 christos } 1724 1.2 christos 1725 1.2 christos 1726 1.2 christos static krb5_error_code 1727 1.2 christos fast_unwrap_as_rep(krb5_context context, int32_t nonce, 1728 1.2 christos krb5_data *chksumdata, 1729 1.2 christos struct fast_state *state, AS_REP *rep) 1730 1.2 christos { 1731 1.2 christos PA_FX_FAST_REPLY fxfastrep; 1732 1.2 christos KrbFastResponse fastrep; 1733 1.2 christos krb5_error_code ret; 1734 1.2 christos PA_DATA *pa = NULL; 1735 1.2 christos int idx = 0; 1736 1.2 christos 1737 1.2 christos if (state->armor_crypto == NULL || rep->padata == NULL) 1738 1.2 christos return check_fast(context, state); 1739 1.2 christos 1740 1.2 christos /* find PA_FX_FAST_REPLY */ 1741 1.2 christos 1742 1.2 christos pa = krb5_find_padata(rep->padata->val, rep->padata->len, 1743 1.2 christos KRB5_PADATA_FX_FAST, &idx); 1744 1.2 christos if (pa == NULL) 1745 1.2 christos return check_fast(context, state); 1746 1.2 christos 1747 1.2 christos memset(&fxfastrep, 0, sizeof(fxfastrep)); 1748 1.2 christos memset(&fastrep, 0, sizeof(fastrep)); 1749 1.2 christos 1750 1.2 christos ret = decode_PA_FX_FAST_REPLY(pa->padata_value.data, pa->padata_value.length, &fxfastrep, NULL); 1751 1.2 christos if (ret) 1752 1.2 christos return ret; 1753 1.2 christos 1754 1.2 christos if (fxfastrep.element == choice_PA_FX_FAST_REPLY_armored_data) { 1755 1.2 christos krb5_data data; 1756 1.2 christos ret = krb5_decrypt_EncryptedData(context, 1757 1.2 christos state->armor_crypto, 1758 1.2 christos KRB5_KU_FAST_REP, 1759 1.2 christos &fxfastrep.u.armored_data.enc_fast_rep, 1760 1.2 christos &data); 1761 1.2 christos if (ret) 1762 1.2 christos goto out; 1763 1.2 christos 1764 1.2 christos ret = decode_KrbFastResponse(data.data, data.length, &fastrep, NULL); 1765 1.2 christos krb5_data_free(&data); 1766 1.2 christos if (ret) 1767 1.2 christos goto out; 1768 1.2 christos 1769 1.2 christos } else { 1770 1.2 christos ret = KRB5KDC_ERR_PREAUTH_FAILED; 1771 1.2 christos goto out; 1772 1.2 christos } 1773 1.2 christos 1774 1.2 christos free_METHOD_DATA(rep->padata); 1775 1.2 christos ret = copy_METHOD_DATA(&fastrep.padata, rep->padata); 1776 1.2 christos if (ret) 1777 1.2 christos goto out; 1778 1.2 christos 1779 1.2 christos if (fastrep.strengthen_key) { 1780 1.2 christos if (state->strengthen_key) 1781 1.2 christos krb5_free_keyblock(context, state->strengthen_key); 1782 1.2 christos 1783 1.2 christos ret = krb5_copy_keyblock(context, fastrep.strengthen_key, &state->strengthen_key); 1784 1.2 christos if (ret) 1785 1.2 christos goto out; 1786 1.2 christos } 1787 1.2 christos 1788 1.2 christos if (nonce != fastrep.nonce) { 1789 1.2 christos ret = KRB5KDC_ERR_PREAUTH_FAILED; 1790 1.2 christos goto out; 1791 1.2 christos } 1792 1.2 christos if (fastrep.finished) { 1793 1.2 christos PrincipalName cname; 1794 1.2 christos krb5_realm crealm = NULL; 1795 1.2 christos 1796 1.2 christos if (chksumdata == NULL) { 1797 1.2 christos ret = KRB5KDC_ERR_PREAUTH_FAILED; 1798 1.2 christos goto out; 1799 1.2 christos } 1800 1.2 christos 1801 1.2 christos ret = krb5_verify_checksum(context, state->armor_crypto, 1802 1.2 christos KRB5_KU_FAST_FINISHED, 1803 1.2 christos chksumdata->data, chksumdata->length, 1804 1.2 christos &fastrep.finished->ticket_checksum); 1805 1.2 christos if (ret) 1806 1.2 christos goto out; 1807 1.2 christos 1808 1.2 christos /* update */ 1809 1.2 christos ret = copy_Realm(&fastrep.finished->crealm, &crealm); 1810 1.2 christos if (ret) 1811 1.2 christos goto out; 1812 1.2 christos free_Realm(&rep->crealm); 1813 1.2 christos rep->crealm = crealm; 1814 1.2 christos 1815 1.2 christos ret = copy_PrincipalName(&fastrep.finished->cname, &cname); 1816 1.2 christos if (ret) 1817 1.2 christos goto out; 1818 1.2 christos free_PrincipalName(&rep->cname); 1819 1.2 christos rep->cname = cname; 1820 1.2 christos 1821 1.2 christos #if 0 /* store authenticated checksum as kdc-offset */ 1822 1.2 christos fastrep->finished.timestamp; 1823 1.2 christos fastrep->finished.usec = 0; 1824 1.2 christos #endif 1825 1.2 christos 1826 1.2 christos } else if (chksumdata) { 1827 1.2 christos /* expected fastrep.finish but didn't get it */ 1828 1.2 christos ret = KRB5KDC_ERR_PREAUTH_FAILED; 1829 1.2 christos } 1830 1.2 christos 1831 1.2 christos out: 1832 1.2 christos free_PA_FX_FAST_REPLY(&fxfastrep); 1833 1.2 christos 1834 1.2 christos return ret; 1835 1.2 christos } 1836 1.2 christos 1837 1.2 christos static krb5_error_code 1838 1.2 christos fast_unwrap_error(krb5_context context, struct fast_state *state, KRB_ERROR *error) 1839 1.2 christos { 1840 1.2 christos if (state->armor_crypto == NULL) 1841 1.2 christos return check_fast(context, state); 1842 1.2 christos 1843 1.2 christos return 0; 1844 1.2 christos } 1845 1.2 christos 1846 1.2 christos krb5_error_code 1847 1.2 christos _krb5_make_fast_ap_fxarmor(krb5_context context, 1848 1.2 christos krb5_ccache armor_ccache, 1849 1.2 christos krb5_data *armor_value, 1850 1.2 christos krb5_keyblock *armor_key, 1851 1.2 christos krb5_crypto *armor_crypto) 1852 1.2 christos { 1853 1.2 christos krb5_auth_context auth_context = NULL; 1854 1.2 christos krb5_creds cred, *credp = NULL; 1855 1.2 christos krb5_error_code ret; 1856 1.2 christos krb5_data empty; 1857 1.2 christos 1858 1.2 christos krb5_data_zero(&empty); 1859 1.2 christos 1860 1.2 christos memset(&cred, 0, sizeof(cred)); 1861 1.2 christos 1862 1.2 christos ret = krb5_auth_con_init (context, &auth_context); 1863 1.2 christos if (ret) 1864 1.2 christos goto out; 1865 1.2 christos 1866 1.2 christos ret = krb5_cc_get_principal(context, armor_ccache, &cred.client); 1867 1.2 christos if (ret) 1868 1.2 christos goto out; 1869 1.2 christos 1870 1.2 christos ret = krb5_make_principal(context, &cred.server, 1871 1.2 christos cred.client->realm, 1872 1.2 christos KRB5_TGS_NAME, 1873 1.2 christos cred.client->realm, 1874 1.2 christos NULL); 1875 1.2 christos if (ret) { 1876 1.2 christos krb5_free_principal(context, cred.client); 1877 1.2 christos goto out; 1878 1.2 christos } 1879 1.2 christos 1880 1.2 christos ret = krb5_get_credentials(context, 0, armor_ccache, &cred, &credp); 1881 1.2 christos krb5_free_principal(context, cred.server); 1882 1.2 christos krb5_free_principal(context, cred.client); 1883 1.2 christos if (ret) 1884 1.2 christos goto out; 1885 1.2 christos 1886 1.2 christos ret = krb5_auth_con_add_AuthorizationData(context, auth_context, KRB5_PADATA_FX_FAST_ARMOR, &empty); 1887 1.2 christos if (ret) 1888 1.2 christos goto out; 1889 1.2 christos 1890 1.2 christos ret = krb5_mk_req_extended(context, 1891 1.2 christos &auth_context, 1892 1.2 christos AP_OPTS_USE_SUBKEY, 1893 1.2 christos NULL, 1894 1.2 christos credp, 1895 1.2 christos armor_value); 1896 1.2 christos krb5_free_creds(context, credp); 1897 1.2 christos if (ret) 1898 1.2 christos goto out; 1899 1.2 christos 1900 1.2 christos ret = _krb5_fast_armor_key(context, 1901 1.2 christos auth_context->local_subkey, 1902 1.2 christos auth_context->keyblock, 1903 1.2 christos armor_key, 1904 1.2 christos armor_crypto); 1905 1.2 christos if (ret) 1906 1.2 christos goto out; 1907 1.2 christos 1908 1.2 christos out: 1909 1.2 christos krb5_auth_con_free(context, auth_context); 1910 1.2 christos return ret; 1911 1.2 christos } 1912 1.2 christos 1913 1.2 christos #ifndef WIN32 1914 1.2 christos static heim_base_once_t armor_service_once = HEIM_BASE_ONCE_INIT; 1915 1.2 christos static heim_ipc armor_service = NULL; 1916 1.2 christos 1917 1.2 christos static void 1918 1.2 christos fast_armor_init_ipc(void *ctx) 1919 1.2 christos { 1920 1.2 christos heim_ipc *ipc = ctx; 1921 1.2 christos heim_ipc_init_context("ANY:org.h5l.armor-service", ipc); 1922 1.2 christos } 1923 1.2 christos #endif /* WIN32 */ 1924 1.2 christos 1925 1.2 christos 1926 1.2 christos static krb5_error_code 1927 1.2 christos make_fast_ap_fxarmor(krb5_context context, 1928 1.2 christos struct fast_state *state, 1929 1.2 christos const char *realm, 1930 1.2 christos KrbFastArmor **armor) 1931 1.2 christos { 1932 1.2 christos KrbFastArmor *fxarmor = NULL; 1933 1.2 christos krb5_error_code ret; 1934 1.2 christos 1935 1.2 christos if (state->armor_crypto) 1936 1.2 christos krb5_crypto_destroy(context, state->armor_crypto); 1937 1.2 christos krb5_free_keyblock_contents(context, &state->armor_key); 1938 1.2 christos 1939 1.2 christos 1940 1.2 christos ALLOC(fxarmor, 1); 1941 1.2 christos if (fxarmor == NULL) 1942 1.2 christos return krb5_enomem(context); 1943 1.2 christos 1944 1.2 christos if (state->flags & KRB5_FAST_AP_ARMOR_SERVICE) { 1945 1.2 christos #ifdef WIN32 1946 1.2 christos krb5_set_error_message(context, ENOTSUP, "Fast armor IPC service not supportted yet on Windows"); 1947 1.2 christos ret = ENOTSUP; 1948 1.2 christos goto out; 1949 1.2 christos #else /* WIN32 */ 1950 1.2 christos KERB_ARMOR_SERVICE_REPLY msg; 1951 1.2 christos krb5_data request, reply; 1952 1.2 christos 1953 1.2 christos heim_base_once_f(&armor_service_once, &armor_service, fast_armor_init_ipc); 1954 1.2 christos if (armor_service == NULL) { 1955 1.2 christos krb5_set_error_message(context, ENOENT, "Failed to open fast armor service"); 1956 1.2 christos ret = ENOENT; 1957 1.2 christos goto out; 1958 1.2 christos } 1959 1.2 christos 1960 1.2 christos krb5_data_zero(&reply); 1961 1.2 christos 1962 1.2 christos request.data = rk_UNCONST(realm); 1963 1.2 christos request.length = strlen(realm); 1964 1.2 christos 1965 1.2 christos ret = heim_ipc_call(armor_service, &request, &reply, NULL); 1966 1.2 christos heim_release(send); 1967 1.2 christos if (ret) { 1968 1.2 christos krb5_set_error_message(context, ret, "Failed to get armor service credential"); 1969 1.2 christos goto out; 1970 1.2 christos } 1971 1.2 christos 1972 1.2 christos ret = decode_KERB_ARMOR_SERVICE_REPLY(reply.data, reply.length, &msg, NULL); 1973 1.2 christos krb5_data_free(&reply); 1974 1.2 christos if (ret) 1975 1.2 christos goto out; 1976 1.2 christos 1977 1.2 christos ret = copy_KrbFastArmor(fxarmor, &msg.armor); 1978 1.2 christos if (ret) { 1979 1.2 christos free_KERB_ARMOR_SERVICE_REPLY(&msg); 1980 1.2 christos goto out; 1981 1.2 christos } 1982 1.2 christos 1983 1.2 christos ret = krb5_copy_keyblock_contents(context, &msg.armor_key, &state->armor_key); 1984 1.2 christos free_KERB_ARMOR_SERVICE_REPLY(&msg); 1985 1.2 christos if (ret) 1986 1.2 christos goto out; 1987 1.2 christos 1988 1.2 christos ret = krb5_crypto_init(context, &state->armor_key, 0, &state->armor_crypto); 1989 1.2 christos if (ret) 1990 1.2 christos goto out; 1991 1.2 christos #endif /* WIN32 */ 1992 1.2 christos } else { 1993 1.2 christos 1994 1.2 christos fxarmor->armor_type = 1; 1995 1.2 christos 1996 1.2 christos ret = _krb5_make_fast_ap_fxarmor(context, 1997 1.2 christos state->armor_ccache, 1998 1.2 christos &fxarmor->armor_value, 1999 1.2 christos &state->armor_key, 2000 1.2 christos &state->armor_crypto); 2001 1.2 christos if (ret) 2002 1.2 christos goto out; 2003 1.2 christos } 2004 1.2 christos 2005 1.2 christos 2006 1.2 christos *armor = fxarmor; 2007 1.2 christos fxarmor = NULL; 2008 1.2 christos out: 2009 1.2 christos if (fxarmor) { 2010 1.2 christos free_KrbFastArmor(fxarmor); 2011 1.2 christos free(fxarmor); 2012 1.2 christos } 2013 1.2 christos return ret; 2014 1.2 christos } 2015 1.2 christos 2016 1.2 christos static krb5_error_code 2017 1.2 christos fast_wrap_req(krb5_context context, struct fast_state *state, KDC_REQ *req) 2018 1.2 christos { 2019 1.2 christos KrbFastArmor *fxarmor = NULL; 2020 1.2 christos PA_FX_FAST_REQUEST fxreq; 2021 1.2 christos krb5_error_code ret; 2022 1.2 christos KrbFastReq fastreq; 2023 1.2 christos krb5_data data; 2024 1.2 christos size_t size; 2025 1.2 christos 2026 1.2 christos if (state->flags & KRB5_FAST_DISABLED) { 2027 1.2 christos _krb5_debug(context, 10, "fast disabled, not doing any fast wrapping"); 2028 1.2 christos return 0; 2029 1.2 christos } 2030 1.2 christos 2031 1.2 christos memset(&fxreq, 0, sizeof(fxreq)); 2032 1.2 christos memset(&fastreq, 0, sizeof(fastreq)); 2033 1.2 christos krb5_data_zero(&data); 2034 1.2 christos 2035 1.2 christos if (state->armor_crypto == NULL) { 2036 1.2 christos if (state->armor_ccache) { 2037 1.2 christos /* 2038 1.2 christos * Instead of keeping state in FX_COOKIE in the KDC, we 2039 1.2 christos * rebuild a new armor key for every request, because this 2040 1.2 christos * is what the MIT KDC expect and RFC6113 is vage about 2041 1.2 christos * what the behavior should be. 2042 1.2 christos */ 2043 1.2 christos state->type = choice_PA_FX_FAST_REQUEST_armored_data; 2044 1.2 christos } else { 2045 1.2 christos return check_fast(context, state); 2046 1.2 christos } 2047 1.2 christos } 2048 1.2 christos 2049 1.2 christos state->flags |= KRB5_FAST_EXPECTED; 2050 1.2 christos 2051 1.2 christos fastreq.fast_options.hide_client_names = 1; 2052 1.2 christos 2053 1.2 christos ret = copy_KDC_REQ_BODY(&req->req_body, &fastreq.req_body); 2054 1.2 christos free_KDC_REQ_BODY(&req->req_body); 2055 1.2 christos 2056 1.2 christos req->req_body.realm = strdup(KRB5_ANON_REALM); 2057 1.2 christos if ((ALLOC(req->req_body.cname, 1)) != NULL) { 2058 1.2 christos req->req_body.cname->name_type = KRB5_NT_WELLKNOWN; 2059 1.2 christos if ((ALLOC(req->req_body.cname->name_string.val, 2)) != NULL) { 2060 1.2 christos req->req_body.cname->name_string.len = 2; 2061 1.2 christos req->req_body.cname->name_string.val[0] = strdup(KRB5_WELLKNOWN_NAME); 2062 1.2 christos req->req_body.cname->name_string.val[1] = strdup(KRB5_ANON_NAME); 2063 1.2 christos if (req->req_body.cname->name_string.val[0] == NULL || 2064 1.2 christos req->req_body.cname->name_string.val[1] == NULL) 2065 1.2 christos ret = krb5_enomem(context); 2066 1.2 christos } else 2067 1.2 christos ret = krb5_enomem(context); 2068 1.2 christos } else 2069 1.2 christos ret = krb5_enomem(context); 2070 1.2 christos if ((ALLOC(req->req_body.till, 1)) != NULL) 2071 1.2 christos *req->req_body.till = 0; 2072 1.2 christos else 2073 1.2 christos ret = krb5_enomem(context); 2074 1.2 christos if (ret) 2075 1.2 christos goto out; 2076 1.2 christos 2077 1.2 christos if (req->padata) { 2078 1.2 christos ret = copy_METHOD_DATA(req->padata, &fastreq.padata); 2079 1.2 christos free_METHOD_DATA(req->padata); 2080 1.2 christos } else { 2081 1.2 christos if ((ALLOC(req->padata, 1)) == NULL) 2082 1.2 christos ret = krb5_enomem(context); 2083 1.2 christos } 2084 1.2 christos if (ret) 2085 1.2 christos goto out; 2086 1.2 christos 2087 1.2 christos ASN1_MALLOC_ENCODE(KrbFastReq, data.data, data.length, &fastreq, &size, ret); 2088 1.2 christos if (ret) 2089 1.2 christos goto out; 2090 1.2 christos heim_assert(data.length == size, "ASN.1 internal error"); 2091 1.2 christos 2092 1.2 christos fxreq.element = state->type; 2093 1.2 christos 2094 1.2 christos if (state->type == choice_PA_FX_FAST_REQUEST_armored_data) { 2095 1.2 christos size_t len; 2096 1.2 christos void *buf; 2097 1.2 christos 2098 1.2 christos ret = make_fast_ap_fxarmor(context, state, fastreq.req_body.realm, &fxreq.u.armored_data.armor); 2099 1.2 christos if (ret) 2100 1.2 christos goto out; 2101 1.2 christos 2102 1.2 christos heim_assert(state->armor_crypto != NULL, "FAST armor key missing when FAST started"); 2103 1.2 christos 2104 1.2 christos ASN1_MALLOC_ENCODE(KDC_REQ_BODY, buf, len, &req->req_body, &size, ret); 2105 1.2 christos if (ret) 2106 1.2 christos goto out; 2107 1.2 christos heim_assert(len == size, "ASN.1 internal error"); 2108 1.2 christos 2109 1.2 christos ret = krb5_create_checksum(context, state->armor_crypto, 2110 1.2 christos KRB5_KU_FAST_REQ_CHKSUM, 0, 2111 1.2 christos buf, len, 2112 1.2 christos &fxreq.u.armored_data.req_checksum); 2113 1.2 christos free(buf); 2114 1.2 christos if (ret) 2115 1.2 christos goto out; 2116 1.2 christos 2117 1.2 christos ret = krb5_encrypt_EncryptedData(context, state->armor_crypto, 2118 1.2 christos KRB5_KU_FAST_ENC, 2119 1.2 christos data.data, 2120 1.2 christos data.length, 2121 1.2 christos 0, 2122 1.2 christos &fxreq.u.armored_data.enc_fast_req); 2123 1.2 christos krb5_data_free(&data); 2124 1.2 christos if (ret) 2125 1.2 christos goto out; 2126 1.2 christos 2127 1.2 christos } else { 2128 1.2 christos krb5_data_free(&data); 2129 1.2 christos heim_assert(false, "unknown FAST type, internal error"); 2130 1.2 christos } 2131 1.2 christos 2132 1.2 christos ASN1_MALLOC_ENCODE(PA_FX_FAST_REQUEST, data.data, data.length, &fxreq, &size, ret); 2133 1.2 christos if (ret) 2134 1.2 christos goto out; 2135 1.2 christos heim_assert(data.length == size, "ASN.1 internal error"); 2136 1.2 christos 2137 1.2 christos 2138 1.2 christos ret = krb5_padata_add(context, req->padata, KRB5_PADATA_FX_FAST, data.data, data.length); 2139 1.2 christos if (ret) 2140 1.2 christos goto out; 2141 1.2 christos krb5_data_zero(&data); 2142 1.2 christos 2143 1.2 christos out: 2144 1.2 christos free_PA_FX_FAST_REQUEST(&fxreq); 2145 1.2 christos free_KrbFastReq(&fastreq); 2146 1.2 christos if (fxarmor) { 2147 1.2 christos free_KrbFastArmor(fxarmor); 2148 1.2 christos free(fxarmor); 2149 1.2 christos } 2150 1.2 christos krb5_data_free(&data); 2151 1.2 christos 2152 1.2 christos return ret; 2153 1.2 christos } 2154 1.2 christos 2155 1.2 christos 2156 1.1 elric /** 2157 1.1 elric * The core loop if krb5_get_init_creds() function family. Create the 2158 1.2 christos * packets and have the caller send them off to the KDC. 2159 1.1 elric * 2160 1.1 elric * If the caller want all work been done for them, use 2161 1.1 elric * krb5_init_creds_get() instead. 2162 1.1 elric * 2163 1.1 elric * @param context a Kerberos 5 context. 2164 1.1 elric * @param ctx ctx krb5_init_creds_context context. 2165 1.1 elric * @param in input data from KDC, first round it should be reset by krb5_data_zer(). 2166 1.1 elric * @param out reply to KDC. 2167 1.1 elric * @param hostinfo KDC address info, first round it can be NULL. 2168 1.1 elric * @param flags status of the round, if 2169 1.1 elric * KRB5_INIT_CREDS_STEP_FLAG_CONTINUE is set, continue one more round. 2170 1.1 elric * 2171 1.1 elric * @return 0 for success, or an Kerberos 5 error code, see 2172 1.1 elric * krb5_get_error_message(). 2173 1.1 elric * 2174 1.1 elric * @ingroup krb5_credential 2175 1.1 elric */ 2176 1.1 elric 2177 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 2178 1.1 elric krb5_init_creds_step(krb5_context context, 2179 1.1 elric krb5_init_creds_context ctx, 2180 1.1 elric krb5_data *in, 2181 1.1 elric krb5_data *out, 2182 1.1 elric krb5_krbhst_info *hostinfo, 2183 1.1 elric unsigned int *flags) 2184 1.1 elric { 2185 1.1 elric krb5_error_code ret; 2186 1.2 christos size_t len = 0; 2187 1.1 elric size_t size; 2188 1.2 christos AS_REQ req2; 2189 1.1 elric 2190 1.1 elric krb5_data_zero(out); 2191 1.1 elric 2192 1.1 elric if (ctx->as_req.req_body.cname == NULL) { 2193 1.1 elric ret = init_as_req(context, ctx->flags, &ctx->cred, 2194 1.1 elric ctx->addrs, ctx->etypes, &ctx->as_req); 2195 1.1 elric if (ret) { 2196 1.1 elric free_init_creds_ctx(context, ctx); 2197 1.1 elric return ret; 2198 1.1 elric } 2199 1.1 elric } 2200 1.1 elric 2201 1.1 elric #define MAX_PA_COUNTER 10 2202 1.1 elric if (ctx->pa_counter > MAX_PA_COUNTER) { 2203 1.1 elric krb5_set_error_message(context, KRB5_GET_IN_TKT_LOOP, 2204 1.1 elric N_("Looping %d times while getting " 2205 1.1 elric "initial credentials", ""), 2206 1.1 elric ctx->pa_counter); 2207 1.1 elric return KRB5_GET_IN_TKT_LOOP; 2208 1.1 elric } 2209 1.1 elric ctx->pa_counter++; 2210 1.1 elric 2211 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: loop %d", ctx->pa_counter); 2212 1.1 elric 2213 1.1 elric /* Lets process the input packet */ 2214 1.1 elric if (in && in->length) { 2215 1.1 elric krb5_kdc_rep rep; 2216 1.1 elric 2217 1.1 elric memset(&rep, 0, sizeof(rep)); 2218 1.1 elric 2219 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: processing input"); 2220 1.1 elric 2221 1.1 elric ret = decode_AS_REP(in->data, in->length, &rep.kdc_rep, &size); 2222 1.1 elric if (ret == 0) { 2223 1.1 elric unsigned eflags = EXTRACT_TICKET_AS_REQ | EXTRACT_TICKET_TIMESYNC; 2224 1.2 christos krb5_data data; 2225 1.2 christos 2226 1.2 christos /* 2227 1.2 christos * Unwrap AS-REP 2228 1.2 christos */ 2229 1.2 christos ASN1_MALLOC_ENCODE(Ticket, data.data, data.length, 2230 1.2 christos &rep.kdc_rep.ticket, &size, ret); 2231 1.2 christos if (ret) 2232 1.2 christos goto out; 2233 1.2 christos heim_assert(data.length == size, "ASN.1 internal error"); 2234 1.2 christos 2235 1.2 christos ret = fast_unwrap_as_rep(context, ctx->nonce, &data, 2236 1.2 christos &ctx->fast_state, &rep.kdc_rep); 2237 1.2 christos krb5_data_free(&data); 2238 1.2 christos if (ret) 2239 1.2 christos goto out; 2240 1.2 christos 2241 1.2 christos /* 2242 1.2 christos * Now check and extract the ticket 2243 1.2 christos */ 2244 1.1 elric 2245 1.1 elric if (ctx->flags.canonicalize) { 2246 1.1 elric eflags |= EXTRACT_TICKET_ALLOW_SERVER_MISMATCH; 2247 1.1 elric eflags |= EXTRACT_TICKET_MATCH_REALM; 2248 1.1 elric } 2249 1.1 elric if (ctx->ic_flags & KRB5_INIT_CREDS_NO_C_CANON_CHECK) 2250 1.1 elric eflags |= EXTRACT_TICKET_ALLOW_CNAME_MISMATCH; 2251 1.3 christos if (ctx->flags.request_anonymous) 2252 1.3 christos eflags |= EXTRACT_TICKET_MATCH_ANON; 2253 1.1 elric 2254 1.1 elric ret = process_pa_data_to_key(context, ctx, &ctx->cred, 2255 1.2 christos &ctx->as_req, &rep.kdc_rep, 2256 1.2 christos hostinfo, &ctx->fast_state.reply_key); 2257 1.1 elric if (ret) { 2258 1.1 elric free_AS_REP(&rep.kdc_rep); 2259 1.1 elric goto out; 2260 1.1 elric } 2261 1.1 elric 2262 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: extracting ticket"); 2263 1.1 elric 2264 1.1 elric ret = _krb5_extract_ticket(context, 2265 1.1 elric &rep, 2266 1.1 elric &ctx->cred, 2267 1.2 christos ctx->fast_state.reply_key, 2268 1.1 elric NULL, 2269 1.1 elric KRB5_KU_AS_REP_ENC_PART, 2270 1.1 elric NULL, 2271 1.1 elric ctx->nonce, 2272 1.1 elric eflags, 2273 1.2 christos &ctx->req_buffer, 2274 1.1 elric NULL, 2275 1.1 elric NULL); 2276 1.3 christos if (ret == 0 && ctx->pk_init_ctx) { 2277 1.3 christos PA_DATA *pa_pkinit_kx; 2278 1.3 christos int idx = 0; 2279 1.3 christos 2280 1.3 christos pa_pkinit_kx = 2281 1.3 christos krb5_find_padata(rep.kdc_rep.padata->val, 2282 1.3 christos rep.kdc_rep.padata->len, 2283 1.3 christos KRB5_PADATA_PKINIT_KX, 2284 1.3 christos &idx); 2285 1.3 christos 2286 1.3 christos ret = _krb5_pk_kx_confirm(context, ctx->pk_init_ctx, 2287 1.3 christos ctx->fast_state.reply_key, 2288 1.3 christos &ctx->cred.session, 2289 1.3 christos pa_pkinit_kx); 2290 1.3 christos if (ret) 2291 1.3 christos krb5_set_error_message(context, ret, 2292 1.3 christos N_("Failed to confirm PA-PKINIT-KX", "")); 2293 1.3 christos else if (pa_pkinit_kx != NULL) 2294 1.3 christos ctx->ic_flags |= KRB5_INIT_CREDS_PKINIT_KX_VALID; 2295 1.3 christos } 2296 1.2 christos if (ret == 0) 2297 1.2 christos ret = copy_EncKDCRepPart(&rep.enc_part, &ctx->enc_part); 2298 1.1 elric 2299 1.2 christos krb5_free_keyblock(context, ctx->fast_state.reply_key); 2300 1.2 christos ctx->fast_state.reply_key = NULL; 2301 1.1 elric *flags = 0; 2302 1.1 elric 2303 1.1 elric free_AS_REP(&rep.kdc_rep); 2304 1.1 elric free_EncASRepPart(&rep.enc_part); 2305 1.1 elric 2306 1.1 elric return ret; 2307 1.1 elric 2308 1.1 elric } else { 2309 1.1 elric /* let's try to parse it as a KRB-ERROR */ 2310 1.1 elric 2311 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: got an error"); 2312 1.1 elric 2313 1.1 elric free_KRB_ERROR(&ctx->error); 2314 1.1 elric 2315 1.1 elric ret = krb5_rd_error(context, in, &ctx->error); 2316 1.1 elric if(ret && in->length && ((char*)in->data)[0] == 4) 2317 1.1 elric ret = KRB5KRB_AP_ERR_V4_REPLY; 2318 1.1 elric if (ret) { 2319 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: failed to read error"); 2320 1.1 elric goto out; 2321 1.1 elric } 2322 1.1 elric 2323 1.2 christos /* 2324 1.2 christos * Unwrap KRB-ERROR 2325 1.2 christos */ 2326 1.2 christos ret = fast_unwrap_error(context, &ctx->fast_state, &ctx->error); 2327 1.2 christos if (ret) 2328 1.2 christos goto out; 2329 1.2 christos 2330 1.2 christos /* 2331 1.2 christos * 2332 1.2 christos */ 2333 1.2 christos 2334 1.1 elric ret = krb5_error_from_rd_error(context, &ctx->error, &ctx->cred); 2335 1.1 elric 2336 1.1 elric _krb5_debug(context, 5, "krb5_get_init_creds: KRB-ERROR %d", ret); 2337 1.1 elric 2338 1.1 elric /* 2339 1.1 elric * If no preauth was set and KDC requires it, give it one 2340 1.1 elric * more try. 2341 1.1 elric */ 2342 1.1 elric 2343 1.1 elric if (ret == KRB5KDC_ERR_PREAUTH_REQUIRED) { 2344 1.1 elric 2345 1.1 elric free_METHOD_DATA(&ctx->md); 2346 1.3 christos memset_s(&ctx->md, sizeof(ctx->md), 0, sizeof(ctx->md)); 2347 1.1 elric 2348 1.1 elric if (ctx->error.e_data) { 2349 1.1 elric ret = decode_METHOD_DATA(ctx->error.e_data->data, 2350 1.1 elric ctx->error.e_data->length, 2351 1.1 elric &ctx->md, 2352 1.1 elric NULL); 2353 1.1 elric if (ret) 2354 1.1 elric krb5_set_error_message(context, ret, 2355 1.1 elric N_("Failed to decode METHOD-DATA", "")); 2356 1.1 elric } else { 2357 1.1 elric krb5_set_error_message(context, ret, 2358 1.1 elric N_("Preauth required but no preauth " 2359 1.1 elric "options send by KDC", "")); 2360 1.1 elric } 2361 1.1 elric } else if (ret == KRB5KRB_AP_ERR_SKEW && context->kdc_sec_offset == 0) { 2362 1.2 christos /* 2363 1.1 elric * Try adapt to timeskrew when we are using pre-auth, and 2364 1.1 elric * if there was a time skew, try again. 2365 1.1 elric */ 2366 1.1 elric krb5_set_real_time(context, ctx->error.stime, -1); 2367 1.1 elric if (context->kdc_sec_offset) 2368 1.2 christos ret = 0; 2369 1.1 elric 2370 1.1 elric _krb5_debug(context, 10, "init_creds: err skew updateing kdc offset to %d", 2371 1.1 elric context->kdc_sec_offset); 2372 1.1 elric 2373 1.1 elric ctx->used_pa_types = 0; 2374 1.1 elric 2375 1.1 elric } else if (ret == KRB5_KDC_ERR_WRONG_REALM && ctx->flags.canonicalize) { 2376 1.1 elric /* client referal to a new realm */ 2377 1.1 elric 2378 1.1 elric if (ctx->error.crealm == NULL) { 2379 1.1 elric krb5_set_error_message(context, ret, 2380 1.1 elric N_("Got a client referral, not but no realm", "")); 2381 1.1 elric goto out; 2382 1.1 elric } 2383 1.1 elric _krb5_debug(context, 5, 2384 1.1 elric "krb5_get_init_creds: got referal to realm %s", 2385 1.1 elric *ctx->error.crealm); 2386 1.1 elric 2387 1.2 christos ret = krb5_principal_set_realm(context, 2388 1.1 elric ctx->cred.client, 2389 1.1 elric *ctx->error.crealm); 2390 1.2 christos if (ret) 2391 1.2 christos goto out; 2392 1.2 christos 2393 1.2 christos if (krb5_principal_is_krbtgt(context, ctx->cred.server)) { 2394 1.2 christos ret = krb5_init_creds_set_service(context, ctx, NULL); 2395 1.2 christos if (ret) 2396 1.2 christos goto out; 2397 1.2 christos } 2398 1.2 christos 2399 1.2 christos free_AS_REQ(&ctx->as_req); 2400 1.3 christos memset_s(&ctx->as_req, sizeof(ctx->as_req), 0, sizeof(ctx->as_req)); 2401 1.1 elric 2402 1.1 elric ctx->used_pa_types = 0; 2403 1.2 christos } else if (ret == KRB5KDC_ERR_KEY_EXP && ctx->runflags.change_password == 0 && ctx->prompter) { 2404 1.2 christos char buf2[1024]; 2405 1.2 christos 2406 1.2 christos ctx->runflags.change_password = 1; 2407 1.2 christos 2408 1.2 christos ctx->prompter(context, ctx->prompter_data, NULL, N_("Password has expired", ""), 0, NULL); 2409 1.2 christos 2410 1.2 christos 2411 1.2 christos /* try to avoid recursion */ 2412 1.2 christos if (ctx->in_tkt_service != NULL && strcmp(ctx->in_tkt_service, "kadmin/changepw") == 0) 2413 1.2 christos goto out; 2414 1.2 christos 2415 1.2 christos /* don't try to change password where then where none */ 2416 1.2 christos if (ctx->prompter == NULL) 2417 1.2 christos goto out; 2418 1.2 christos 2419 1.2 christos ret = change_password(context, 2420 1.2 christos ctx->cred.client, 2421 1.2 christos ctx->password, 2422 1.2 christos buf2, 2423 1.2 christos sizeof(buf2), 2424 1.2 christos ctx->prompter, 2425 1.2 christos ctx->prompter_data, 2426 1.2 christos NULL); 2427 1.2 christos if (ret) 2428 1.2 christos goto out; 2429 1.2 christos 2430 1.2 christos krb5_init_creds_set_password(context, ctx, buf2); 2431 1.2 christos 2432 1.2 christos ctx->used_pa_types = 0; 2433 1.2 christos ret = 0; 2434 1.2 christos 2435 1.2 christos } else if (ret == KRB5KDC_ERR_PREAUTH_FAILED) { 2436 1.2 christos 2437 1.2 christos if (ctx->fast_state.flags & KRB5_FAST_DISABLED) 2438 1.2 christos goto out; 2439 1.2 christos if (ctx->fast_state.flags & (KRB5_FAST_REQUIRED | KRB5_FAST_EXPECTED)) 2440 1.2 christos goto out; 2441 1.2 christos 2442 1.2 christos _krb5_debug(context, 10, "preauth failed with FAST, " 2443 1.2 christos "and told by KD or user, trying w/o FAST"); 2444 1.2 christos 2445 1.2 christos ctx->fast_state.flags |= KRB5_FAST_DISABLED; 2446 1.2 christos ctx->used_pa_types = 0; 2447 1.2 christos ret = 0; 2448 1.1 elric } 2449 1.1 elric if (ret) 2450 1.1 elric goto out; 2451 1.1 elric } 2452 1.1 elric } 2453 1.1 elric 2454 1.2 christos if (ctx->as_req.req_body.cname == NULL) { 2455 1.2 christos ret = init_as_req(context, ctx->flags, &ctx->cred, 2456 1.2 christos ctx->addrs, ctx->etypes, &ctx->as_req); 2457 1.2 christos if (ret) { 2458 1.2 christos free_init_creds_ctx(context, ctx); 2459 1.2 christos return ret; 2460 1.2 christos } 2461 1.2 christos } 2462 1.2 christos 2463 1.1 elric if (ctx->as_req.padata) { 2464 1.1 elric free_METHOD_DATA(ctx->as_req.padata); 2465 1.1 elric free(ctx->as_req.padata); 2466 1.1 elric ctx->as_req.padata = NULL; 2467 1.1 elric } 2468 1.1 elric 2469 1.1 elric /* Set a new nonce. */ 2470 1.1 elric ctx->as_req.req_body.nonce = ctx->nonce; 2471 1.1 elric 2472 1.1 elric /* fill_in_md_data */ 2473 1.1 elric ret = process_pa_data_to_md(context, &ctx->cred, &ctx->as_req, ctx, 2474 1.1 elric &ctx->md, &ctx->as_req.padata, 2475 1.1 elric ctx->prompter, ctx->prompter_data); 2476 1.1 elric if (ret) 2477 1.1 elric goto out; 2478 1.1 elric 2479 1.2 christos /* 2480 1.2 christos * Wrap with FAST 2481 1.2 christos */ 2482 1.2 christos copy_AS_REQ(&ctx->as_req, &req2); 2483 1.2 christos 2484 1.2 christos ret = fast_wrap_req(context, &ctx->fast_state, &req2); 2485 1.2 christos if (ret) { 2486 1.2 christos free_AS_REQ(&req2); 2487 1.2 christos goto out; 2488 1.2 christos } 2489 1.2 christos 2490 1.1 elric krb5_data_free(&ctx->req_buffer); 2491 1.1 elric 2492 1.1 elric ASN1_MALLOC_ENCODE(AS_REQ, 2493 1.1 elric ctx->req_buffer.data, ctx->req_buffer.length, 2494 1.2 christos &req2, &len, ret); 2495 1.2 christos free_AS_REQ(&req2); 2496 1.1 elric if (ret) 2497 1.1 elric goto out; 2498 1.1 elric if(len != ctx->req_buffer.length) 2499 1.1 elric krb5_abortx(context, "internal error in ASN.1 encoder"); 2500 1.1 elric 2501 1.1 elric out->data = ctx->req_buffer.data; 2502 1.1 elric out->length = ctx->req_buffer.length; 2503 1.1 elric 2504 1.1 elric *flags = KRB5_INIT_CREDS_STEP_FLAG_CONTINUE; 2505 1.1 elric 2506 1.1 elric return 0; 2507 1.1 elric out: 2508 1.1 elric return ret; 2509 1.1 elric } 2510 1.1 elric 2511 1.1 elric /** 2512 1.1 elric * Extract the newly acquired credentials from krb5_init_creds_context 2513 1.1 elric * context. 2514 1.1 elric * 2515 1.1 elric * @param context A Kerberos 5 context. 2516 1.1 elric * @param ctx 2517 1.1 elric * @param cred credentials, free with krb5_free_cred_contents(). 2518 1.1 elric * 2519 1.1 elric * @return 0 for sucess or An Kerberos error code, see krb5_get_error_message(). 2520 1.1 elric */ 2521 1.1 elric 2522 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 2523 1.1 elric krb5_init_creds_get_creds(krb5_context context, 2524 1.1 elric krb5_init_creds_context ctx, 2525 1.1 elric krb5_creds *cred) 2526 1.1 elric { 2527 1.1 elric return krb5_copy_creds_contents(context, &ctx->cred, cred); 2528 1.1 elric } 2529 1.1 elric 2530 1.1 elric /** 2531 1.1 elric * Get the last error from the transaction. 2532 1.1 elric * 2533 1.1 elric * @return Returns 0 or an error code 2534 1.1 elric * 2535 1.1 elric * @ingroup krb5_credential 2536 1.1 elric */ 2537 1.1 elric 2538 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 2539 1.1 elric krb5_init_creds_get_error(krb5_context context, 2540 1.1 elric krb5_init_creds_context ctx, 2541 1.1 elric KRB_ERROR *error) 2542 1.1 elric { 2543 1.1 elric krb5_error_code ret; 2544 1.1 elric 2545 1.1 elric ret = copy_KRB_ERROR(&ctx->error, error); 2546 1.1 elric if (ret) 2547 1.2 christos krb5_enomem(context); 2548 1.2 christos 2549 1.2 christos return ret; 2550 1.2 christos } 2551 1.2 christos 2552 1.2 christos /** 2553 1.2 christos * 2554 1.2 christos * @ingroup krb5_credential 2555 1.2 christos */ 2556 1.2 christos 2557 1.2 christos krb5_error_code 2558 1.2 christos krb5_init_creds_store(krb5_context context, 2559 1.2 christos krb5_init_creds_context ctx, 2560 1.2 christos krb5_ccache id) 2561 1.2 christos { 2562 1.2 christos krb5_error_code ret; 2563 1.2 christos 2564 1.2 christos if (ctx->cred.client == NULL) { 2565 1.2 christos ret = KRB5KDC_ERR_PREAUTH_REQUIRED; 2566 1.2 christos krb5_set_error_message(context, ret, "init creds not completed yet"); 2567 1.2 christos return ret; 2568 1.2 christos } 2569 1.2 christos 2570 1.2 christos ret = krb5_cc_initialize(context, id, ctx->cred.client); 2571 1.2 christos if (ret) 2572 1.2 christos return ret; 2573 1.2 christos 2574 1.2 christos ret = krb5_cc_store_cred(context, id, &ctx->cred); 2575 1.2 christos if (ret) 2576 1.2 christos return ret; 2577 1.2 christos 2578 1.2 christos if (ctx->cred.flags.b.enc_pa_rep) { 2579 1.2 christos krb5_data data = { 3, rk_UNCONST("yes") }; 2580 1.2 christos ret = krb5_cc_set_config(context, id, ctx->cred.server, 2581 1.2 christos "fast_avail", &data); 2582 1.2 christos if (ret) 2583 1.2 christos return ret; 2584 1.2 christos } 2585 1.1 elric 2586 1.1 elric return ret; 2587 1.1 elric } 2588 1.1 elric 2589 1.1 elric /** 2590 1.1 elric * Free the krb5_init_creds_context allocated by krb5_init_creds_init(). 2591 1.1 elric * 2592 1.1 elric * @param context A Kerberos 5 context. 2593 1.1 elric * @param ctx The krb5_init_creds_context to free. 2594 1.1 elric * 2595 1.1 elric * @ingroup krb5_credential 2596 1.1 elric */ 2597 1.1 elric 2598 1.1 elric KRB5_LIB_FUNCTION void KRB5_LIB_CALL 2599 1.1 elric krb5_init_creds_free(krb5_context context, 2600 1.1 elric krb5_init_creds_context ctx) 2601 1.1 elric { 2602 1.1 elric free_init_creds_ctx(context, ctx); 2603 1.1 elric free(ctx); 2604 1.1 elric } 2605 1.1 elric 2606 1.1 elric /** 2607 1.1 elric * Get new credentials as setup by the krb5_init_creds_context. 2608 1.1 elric * 2609 1.1 elric * @param context A Kerberos 5 context. 2610 1.1 elric * @param ctx The krb5_init_creds_context to process. 2611 1.1 elric * 2612 1.1 elric * @ingroup krb5_credential 2613 1.1 elric */ 2614 1.1 elric 2615 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 2616 1.1 elric krb5_init_creds_get(krb5_context context, krb5_init_creds_context ctx) 2617 1.1 elric { 2618 1.1 elric krb5_sendto_ctx stctx = NULL; 2619 1.1 elric krb5_krbhst_info *hostinfo = NULL; 2620 1.1 elric krb5_error_code ret; 2621 1.1 elric krb5_data in, out; 2622 1.1 elric unsigned int flags = 0; 2623 1.1 elric 2624 1.1 elric krb5_data_zero(&in); 2625 1.1 elric krb5_data_zero(&out); 2626 1.1 elric 2627 1.1 elric ret = krb5_sendto_ctx_alloc(context, &stctx); 2628 1.1 elric if (ret) 2629 1.1 elric goto out; 2630 1.1 elric krb5_sendto_ctx_set_func(stctx, _krb5_kdc_retry, NULL); 2631 1.1 elric 2632 1.1 elric while (1) { 2633 1.1 elric flags = 0; 2634 1.1 elric ret = krb5_init_creds_step(context, ctx, &in, &out, hostinfo, &flags); 2635 1.1 elric krb5_data_free(&in); 2636 1.1 elric if (ret) 2637 1.1 elric goto out; 2638 1.1 elric 2639 1.1 elric if ((flags & 1) == 0) 2640 1.1 elric break; 2641 1.1 elric 2642 1.2 christos ret = krb5_sendto_context (context, stctx, &out, 2643 1.1 elric ctx->cred.client->realm, &in); 2644 1.1 elric if (ret) 2645 1.1 elric goto out; 2646 1.1 elric 2647 1.1 elric } 2648 1.1 elric 2649 1.1 elric out: 2650 1.1 elric if (stctx) 2651 1.1 elric krb5_sendto_ctx_free(context, stctx); 2652 1.1 elric 2653 1.1 elric return ret; 2654 1.1 elric } 2655 1.1 elric 2656 1.1 elric /** 2657 1.1 elric * Get new credentials using password. 2658 1.1 elric * 2659 1.1 elric * @ingroup krb5_credential 2660 1.1 elric */ 2661 1.1 elric 2662 1.1 elric 2663 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 2664 1.1 elric krb5_get_init_creds_password(krb5_context context, 2665 1.1 elric krb5_creds *creds, 2666 1.1 elric krb5_principal client, 2667 1.1 elric const char *password, 2668 1.1 elric krb5_prompter_fct prompter, 2669 1.1 elric void *data, 2670 1.1 elric krb5_deltat start_time, 2671 1.1 elric const char *in_tkt_service, 2672 1.1 elric krb5_get_init_creds_opt *options) 2673 1.1 elric { 2674 1.1 elric krb5_init_creds_context ctx; 2675 1.2 christos char buf[BUFSIZ], buf2[BUFSIZ]; 2676 1.1 elric krb5_error_code ret; 2677 1.1 elric int chpw = 0; 2678 1.1 elric 2679 1.1 elric again: 2680 1.1 elric ret = krb5_init_creds_init(context, client, prompter, data, start_time, options, &ctx); 2681 1.1 elric if (ret) 2682 1.1 elric goto out; 2683 1.1 elric 2684 1.1 elric ret = krb5_init_creds_set_service(context, ctx, in_tkt_service); 2685 1.1 elric if (ret) 2686 1.1 elric goto out; 2687 1.1 elric 2688 1.1 elric if (prompter != NULL && ctx->password == NULL && password == NULL) { 2689 1.1 elric krb5_prompt prompt; 2690 1.1 elric krb5_data password_data; 2691 1.2 christos char *p, *q = NULL; 2692 1.2 christos int aret; 2693 1.2 christos 2694 1.2 christos ret = krb5_unparse_name(context, client, &p); 2695 1.2 christos if (ret) 2696 1.2 christos goto out; 2697 1.1 elric 2698 1.2 christos aret = asprintf(&q, "%s's Password: ", p); 2699 1.1 elric free (p); 2700 1.2 christos if (aret == -1 || q == NULL) { 2701 1.2 christos ret = krb5_enomem(context); 2702 1.2 christos goto out; 2703 1.2 christos } 2704 1.1 elric prompt.prompt = q; 2705 1.1 elric password_data.data = buf; 2706 1.1 elric password_data.length = sizeof(buf); 2707 1.1 elric prompt.hidden = 1; 2708 1.1 elric prompt.reply = &password_data; 2709 1.1 elric prompt.type = KRB5_PROMPT_TYPE_PASSWORD; 2710 1.1 elric 2711 1.1 elric ret = (*prompter) (context, data, NULL, NULL, 1, &prompt); 2712 1.1 elric free (q); 2713 1.1 elric if (ret) { 2714 1.3 christos memset_s(buf, sizeof(buf), 0, sizeof(buf)); 2715 1.1 elric ret = KRB5_LIBOS_PWDINTR; 2716 1.1 elric krb5_clear_error_message (context); 2717 1.1 elric goto out; 2718 1.1 elric } 2719 1.1 elric password = password_data.data; 2720 1.1 elric } 2721 1.1 elric 2722 1.1 elric if (password) { 2723 1.1 elric ret = krb5_init_creds_set_password(context, ctx, password); 2724 1.1 elric if (ret) 2725 1.1 elric goto out; 2726 1.1 elric } 2727 1.1 elric 2728 1.1 elric ret = krb5_init_creds_get(context, ctx); 2729 1.2 christos 2730 1.1 elric if (ret == 0) 2731 1.2 christos krb5_process_last_request(context, options, ctx); 2732 1.1 elric 2733 1.1 elric 2734 1.1 elric if (ret == KRB5KDC_ERR_KEY_EXPIRED && chpw == 0) { 2735 1.1 elric /* try to avoid recursion */ 2736 1.1 elric if (in_tkt_service != NULL && strcmp(in_tkt_service, "kadmin/changepw") == 0) 2737 1.1 elric goto out; 2738 1.1 elric 2739 1.1 elric /* don't try to change password where then where none */ 2740 1.1 elric if (prompter == NULL) 2741 1.1 elric goto out; 2742 1.1 elric 2743 1.2 christos if ((options->flags & KRB5_GET_INIT_CREDS_OPT_CHANGE_PASSWORD_PROMPT) && 2744 1.2 christos !options->change_password_prompt) 2745 1.2 christos goto out; 2746 1.2 christos 2747 1.1 elric ret = change_password (context, 2748 1.1 elric client, 2749 1.1 elric ctx->password, 2750 1.1 elric buf2, 2751 1.2 christos sizeof(buf2), 2752 1.1 elric prompter, 2753 1.1 elric data, 2754 1.1 elric options); 2755 1.1 elric if (ret) 2756 1.1 elric goto out; 2757 1.2 christos password = buf2; 2758 1.1 elric chpw = 1; 2759 1.1 elric krb5_init_creds_free(context, ctx); 2760 1.1 elric goto again; 2761 1.1 elric } 2762 1.1 elric 2763 1.1 elric out: 2764 1.1 elric if (ret == 0) 2765 1.1 elric krb5_init_creds_get_creds(context, ctx, creds); 2766 1.1 elric 2767 1.1 elric if (ctx) 2768 1.1 elric krb5_init_creds_free(context, ctx); 2769 1.1 elric 2770 1.3 christos memset_s(buf, sizeof(buf), 0, sizeof(buf)); 2771 1.3 christos memset_s(buf2, sizeof(buf), 0, sizeof(buf2)); 2772 1.1 elric return ret; 2773 1.1 elric } 2774 1.1 elric 2775 1.1 elric /** 2776 1.1 elric * Get new credentials using keyblock. 2777 1.1 elric * 2778 1.1 elric * @ingroup krb5_credential 2779 1.1 elric */ 2780 1.1 elric 2781 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 2782 1.1 elric krb5_get_init_creds_keyblock(krb5_context context, 2783 1.1 elric krb5_creds *creds, 2784 1.1 elric krb5_principal client, 2785 1.1 elric krb5_keyblock *keyblock, 2786 1.1 elric krb5_deltat start_time, 2787 1.1 elric const char *in_tkt_service, 2788 1.1 elric krb5_get_init_creds_opt *options) 2789 1.1 elric { 2790 1.1 elric krb5_init_creds_context ctx; 2791 1.1 elric krb5_error_code ret; 2792 1.1 elric 2793 1.1 elric memset(creds, 0, sizeof(*creds)); 2794 1.1 elric 2795 1.1 elric ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx); 2796 1.1 elric if (ret) 2797 1.1 elric goto out; 2798 1.1 elric 2799 1.1 elric ret = krb5_init_creds_set_service(context, ctx, in_tkt_service); 2800 1.1 elric if (ret) 2801 1.1 elric goto out; 2802 1.1 elric 2803 1.1 elric ret = krb5_init_creds_set_keyblock(context, ctx, keyblock); 2804 1.1 elric if (ret) 2805 1.1 elric goto out; 2806 1.1 elric 2807 1.1 elric ret = krb5_init_creds_get(context, ctx); 2808 1.1 elric 2809 1.1 elric if (ret == 0) 2810 1.2 christos krb5_process_last_request(context, options, ctx); 2811 1.1 elric 2812 1.1 elric out: 2813 1.1 elric if (ret == 0) 2814 1.1 elric krb5_init_creds_get_creds(context, ctx, creds); 2815 1.1 elric 2816 1.1 elric if (ctx) 2817 1.1 elric krb5_init_creds_free(context, ctx); 2818 1.1 elric 2819 1.1 elric return ret; 2820 1.1 elric } 2821 1.1 elric 2822 1.1 elric /** 2823 1.1 elric * Get new credentials using keytab. 2824 1.1 elric * 2825 1.1 elric * @ingroup krb5_credential 2826 1.1 elric */ 2827 1.1 elric 2828 1.1 elric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL 2829 1.1 elric krb5_get_init_creds_keytab(krb5_context context, 2830 1.1 elric krb5_creds *creds, 2831 1.1 elric krb5_principal client, 2832 1.1 elric krb5_keytab keytab, 2833 1.1 elric krb5_deltat start_time, 2834 1.1 elric const char *in_tkt_service, 2835 1.1 elric krb5_get_init_creds_opt *options) 2836 1.1 elric { 2837 1.1 elric krb5_init_creds_context ctx; 2838 1.2 christos krb5_keytab_entry ktent; 2839 1.1 elric krb5_error_code ret; 2840 1.1 elric 2841 1.2 christos memset(&ktent, 0, sizeof(ktent)); 2842 1.1 elric memset(creds, 0, sizeof(*creds)); 2843 1.1 elric 2844 1.2 christos if (strcmp(client->realm, "") == 0) { 2845 1.2 christos /* 2846 1.2 christos * Referral realm. We have a keytab, so pick a realm by 2847 1.2 christos * matching in the keytab. 2848 1.2 christos */ 2849 1.2 christos ret = krb5_kt_get_entry(context, keytab, client, 0, 0, &ktent); 2850 1.2 christos if (ret == 0) 2851 1.2 christos client = ktent.principal; 2852 1.2 christos } 2853 1.2 christos 2854 1.1 elric ret = krb5_init_creds_init(context, client, NULL, NULL, start_time, options, &ctx); 2855 1.1 elric if (ret) 2856 1.1 elric goto out; 2857 1.1 elric 2858 1.1 elric ret = krb5_init_creds_set_service(context, ctx, in_tkt_service); 2859 1.1 elric if (ret) 2860 1.1 elric goto out; 2861 1.1 elric 2862 1.1 elric ret = krb5_init_creds_set_keytab(context, ctx, keytab); 2863 1.1 elric if (ret) 2864 1.1 elric goto out; 2865 1.1 elric 2866 1.1 elric ret = krb5_init_creds_get(context, ctx); 2867 1.1 elric if (ret == 0) 2868 1.2 christos krb5_process_last_request(context, options, ctx); 2869 1.1 elric 2870 1.1 elric out: 2871 1.2 christos krb5_kt_free_entry(context, &ktent); 2872 1.1 elric if (ret == 0) 2873 1.1 elric krb5_init_creds_get_creds(context, ctx, creds); 2874 1.1 elric 2875 1.1 elric if (ctx) 2876 1.1 elric krb5_init_creds_free(context, ctx); 2877 1.1 elric 2878 1.1 elric return ret; 2879 1.1 elric } 2880