1/* 2 * Copyright (c) 1997 - 2011 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of the Institute nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include "hdb_locl.h" 35 36/* 37 * free all the memory used by (len, keys) 38 */ 39 40void 41hdb_free_keys(krb5_context context, size_t len, Key *keys) 42{ 43 size_t i; 44 45 for (i = 0; i < len; i++) { 46 free(keys[i].mkvno); 47 keys[i].mkvno = NULL; 48 if (keys[i].salt != NULL) { 49 free_Salt(keys[i].salt); 50 free(keys[i].salt); 51 keys[i].salt = NULL; 52 } 53 krb5_free_keyblock_contents(context, &keys[i].key); 54 } 55 free (keys); 56} 57 58/* 59 * for each entry in `default_keys' try to parse it as a sequence 60 * of etype:salttype:salt, syntax of this if something like: 61 * [(des|des3|etype):](pw-salt|afs3)[:string], if etype is omitted it 62 * means all etypes, and if string is omitted is means the default 63 * string (for that principal). Additional special values: 64 * v5 == pw-salt, and 65 * v4 == des:pw-salt: 66 * afs or afs3 == des:afs3-salt 67 */ 68 69static const krb5_enctype des_etypes[] = { 70 KRB5_ENCTYPE_DES_CBC_MD5, 71 KRB5_ENCTYPE_DES_CBC_MD4, 72 KRB5_ENCTYPE_DES_CBC_CRC 73}; 74 75static const krb5_enctype all_etypes[] = { 76 KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96, 77 KRB5_ENCTYPE_ARCFOUR_HMAC_MD5, 78 KRB5_ENCTYPE_DES3_CBC_SHA1 79}; 80 81static krb5_error_code 82parse_key_set(krb5_context context, const char *key, 83 krb5_enctype **ret_enctypes, size_t *ret_num_enctypes, 84 krb5_salt *salt, krb5_principal principal) 85{ 86 const char *p; 87 char buf[3][256]; 88 int num_buf = 0; 89 int i, num_enctypes = 0; 90 krb5_enctype e; 91 const krb5_enctype *enctypes = NULL; 92 krb5_error_code ret; 93 94 p = key; 95 96 *ret_enctypes = NULL; 97 *ret_num_enctypes = 0; 98 99 /* split p in a list of :-separated strings */ 100 for(num_buf = 0; num_buf < 3; num_buf++) 101 if(strsep_copy(&p, ":", buf[num_buf], sizeof(buf[num_buf])) == -1) 102 break; 103 104 salt->saltvalue.data = NULL; 105 salt->saltvalue.length = 0; 106 107 for(i = 0; i < num_buf; i++) { 108 if(enctypes == NULL && num_buf > 1) { 109 /* this might be a etype specifier */ 110 /* XXX there should be a string_to_etypes handling 111 special cases like `des' and `all' */ 112 if(strcmp(buf[i], "des") == 0) { 113 enctypes = des_etypes; 114 num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]); 115 } else if(strcmp(buf[i], "des3") == 0) { 116 e = KRB5_ENCTYPE_DES3_CBC_SHA1; 117 enctypes = &e; 118 num_enctypes = 1; 119 } else { 120 ret = krb5_string_to_enctype(context, buf[i], &e); 121 if (ret == 0) { 122 enctypes = &e; 123 num_enctypes = 1; 124 } else 125 return ret; 126 } 127 continue; 128 } 129 if(salt->salttype == 0) { 130 /* interpret string as a salt specifier, if no etype 131 is set, this sets default values */ 132 /* XXX should perhaps use string_to_salttype, but that 133 interface sucks */ 134 if(strcmp(buf[i], "pw-salt") == 0) { 135 if(enctypes == NULL) { 136 enctypes = all_etypes; 137 num_enctypes = sizeof(all_etypes)/sizeof(all_etypes[0]); 138 } 139 salt->salttype = KRB5_PW_SALT; 140 } else if(strcmp(buf[i], "afs3-salt") == 0) { 141 if(enctypes == NULL) { 142 enctypes = des_etypes; 143 num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]); 144 } 145 salt->salttype = KRB5_AFS3_SALT; 146 } 147 continue; 148 } 149 150 { 151 /* if there is a final string, use it as the string to 152 salt with, this is mostly useful with null salt for 153 v4 compat, and a cell name for afs compat */ 154 salt->saltvalue.data = strdup(buf[i]); 155 if (salt->saltvalue.data == NULL) { 156 krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); 157 return ENOMEM; 158 } 159 salt->saltvalue.length = strlen(buf[i]); 160 } 161 } 162 163 if(enctypes == NULL || salt->salttype == 0) { 164 krb5_set_error_message(context, EINVAL, "bad value for default_keys `%s'", key); 165 return EINVAL; 166 } 167 168 /* if no salt was specified make up default salt */ 169 if(salt->saltvalue.data == NULL) { 170 if(salt->salttype == KRB5_PW_SALT) { 171 ret = krb5_get_pw_salt(context, principal, salt); 172 if (ret) 173 return ret; 174 } else if(salt->salttype == KRB5_AFS3_SALT) { 175 krb5_const_realm realm = krb5_principal_get_realm(context, principal); 176 salt->saltvalue.data = strdup(realm); 177 if(salt->saltvalue.data == NULL) { 178 krb5_set_error_message(context, ENOMEM, 179 "out of memory while " 180 "parsing salt specifiers"); 181 return ENOMEM; 182 } 183 strlwr(salt->saltvalue.data); 184 salt->saltvalue.length = strlen(realm); 185 } 186 } 187 188 *ret_enctypes = malloc(sizeof(enctypes[0]) * num_enctypes); 189 if (*ret_enctypes == NULL) { 190 krb5_free_salt(context, *salt); 191 krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); 192 return ENOMEM; 193 } 194 memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * num_enctypes); 195 *ret_num_enctypes = num_enctypes; 196 197 return 0; 198} 199 200 201/** 202 * This function adds an HDB entry's current keyset to the entry's key 203 * history. The current keyset is left alone; the caller is responsible 204 * for freeing it. 205 * 206 * @param context Context 207 * @param entry HDB entry 208 */ 209krb5_error_code 210hdb_add_current_keys_to_history(krb5_context context, hdb_entry *entry) 211{ 212 krb5_boolean replace = FALSE; 213 krb5_error_code ret; 214 HDB_extension *ext; 215 hdb_keyset newkeyset; 216 time_t newtime; 217 218 if (entry->keys.len == 0) 219 return 0; /* nothing to do */ 220 221 ext = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); 222 if (ext == NULL) { 223 replace = TRUE; 224 ext = calloc(1, sizeof (*ext)); 225 if (ext == NULL) 226 return krb5_enomem(context); 227 228 ext->data.element = choice_HDB_extension_data_hist_keys; 229 } 230 231 /* 232 * Copy in newest old keyset 233 */ 234 ret = hdb_entry_get_pw_change_time(entry, &newtime); 235 if (ret) 236 goto out; 237 238 memset(&newkeyset, 0, sizeof(newkeyset)); 239 newkeyset.keys = entry->keys; 240 newkeyset.kvno = entry->kvno; 241 newkeyset.set_time = &newtime; 242 243 ret = add_HDB_Ext_KeySet(&ext->data.u.hist_keys, &newkeyset); 244 if (ret) 245 goto out; 246 247 if (replace) { 248 /* hdb_replace_extension() deep-copies ext; what a waste */ 249 ret = hdb_replace_extension(context, entry, ext); 250 if (ret) 251 goto out; 252 } 253 254 out: 255 if (replace && ext) { 256 free_HDB_extension(ext); 257 free(ext); 258 } 259 return ret; 260} 261 262/** 263 * This function adds a key to an HDB entry's key history. 264 * 265 * @param context Context 266 * @param entry HDB entry 267 * @param kvno Key version number of the key to add to the history 268 * @param key The Key to add 269 */ 270krb5_error_code 271hdb_add_history_key(krb5_context context, hdb_entry *entry, krb5_kvno kvno, Key *key) 272{ 273 size_t i; 274 hdb_keyset keyset; 275 HDB_Ext_KeySet *hist_keys; 276 HDB_extension ext; 277 HDB_extension *extp; 278 krb5_error_code ret; 279 280 memset(&keyset, 0, sizeof (keyset)); 281 memset(&ext, 0, sizeof (ext)); 282 283 extp = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); 284 if (extp == NULL) { 285 ext.data.element = choice_HDB_extension_data_hist_keys; 286 extp = &ext; 287 } 288 289 hist_keys = &extp->data.u.hist_keys; 290 291 for (i = 0; i < hist_keys->len; i++) { 292 if (hist_keys->val[i].kvno == kvno) { 293 ret = add_Keys(&hist_keys->val[i].keys, key); 294 goto out; 295 } 296 } 297 298 keyset.kvno = kvno; 299 ret = add_Keys(&keyset.keys, key); 300 if (ret) 301 goto out; 302 ret = add_HDB_Ext_KeySet(hist_keys, &keyset); 303 if (ret) 304 goto out; 305 if (extp == &ext) { 306 ret = hdb_replace_extension(context, entry, &ext); 307 if (ret) 308 goto out; 309 } 310 311out: 312 free_hdb_keyset(&keyset); 313 free_HDB_extension(&ext); 314 return ret; 315} 316 317 318/** 319 * This function changes an hdb_entry's kvno, swapping the current key 320 * set with a historical keyset. If no historical keys are found then 321 * an error is returned (the caller can still set entry->kvno directly). 322 * 323 * @param context krb5_context 324 * @param new_kvno New kvno for the entry 325 * @param entry hdb_entry to modify 326 */ 327krb5_error_code 328hdb_change_kvno(krb5_context context, krb5_kvno new_kvno, hdb_entry *entry) 329{ 330 HDB_extension ext; 331 HDB_extension *extp; 332 hdb_keyset keyset; 333 HDB_Ext_KeySet *hist_keys; 334 unsigned int i; 335 int found = 0; 336 krb5_error_code ret; 337 338 if (entry->kvno == new_kvno) 339 return 0; 340 341 extp = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); 342 if (extp == NULL) { 343 memset(&ext, 0, sizeof (ext)); 344 ext.data.element = choice_HDB_extension_data_hist_keys; 345 extp = &ext; 346 } 347 348 memset(&keyset, 0, sizeof (keyset)); 349 hist_keys = &extp->data.u.hist_keys; 350 for (i = 0; i < hist_keys->len; i++) { 351 if (hist_keys->val[i].kvno == new_kvno) { 352 found = 1; 353 ret = copy_hdb_keyset(&hist_keys->val[i], &keyset); 354 if (ret) 355 goto out; 356 ret = remove_HDB_Ext_KeySet(hist_keys, i); 357 if (ret) 358 goto out; 359 break; 360 } 361 } 362 363 if (!found) 364 return HDB_ERR_KVNO_NOT_FOUND; 365 366 ret = hdb_add_current_keys_to_history(context, entry); 367 if (ret) 368 goto out; 369 370 /* Note: we do nothing with keyset.set_time */ 371 entry->kvno = new_kvno; 372 entry->keys = keyset.keys; /* shortcut */ 373 memset(&keyset.keys, 0, sizeof (keyset.keys)); 374 375out: 376 free_hdb_keyset(&keyset); 377 return ret; 378} 379 380 381static krb5_error_code 382add_enctype_to_key_set(Key **key_set, size_t *nkeyset, 383 krb5_enctype enctype, krb5_salt *salt) 384{ 385 krb5_error_code ret; 386 Key key, *tmp; 387 388 memset(&key, 0, sizeof(key)); 389 390 tmp = realloc(*key_set, (*nkeyset + 1) * sizeof((*key_set)[0])); 391 if (tmp == NULL) 392 return ENOMEM; 393 394 *key_set = tmp; 395 396 key.key.keytype = enctype; 397 key.key.keyvalue.length = 0; 398 key.key.keyvalue.data = NULL; 399 400 if (salt) { 401 key.salt = calloc(1, sizeof(*key.salt)); 402 if (key.salt == NULL) { 403 free_Key(&key); 404 return ENOMEM; 405 } 406 407 key.salt->type = salt->salttype; 408 krb5_data_zero (&key.salt->salt); 409 410 ret = krb5_data_copy(&key.salt->salt, 411 salt->saltvalue.data, 412 salt->saltvalue.length); 413 if (ret) { 414 free_Key(&key); 415 return ret; 416 } 417 } else 418 key.salt = NULL; 419 420 (*key_set)[*nkeyset] = key; 421 422 *nkeyset += 1; 423 424 return 0; 425} 426 427 428static 429krb5_error_code 430ks_tuple2str(krb5_context context, int n_ks_tuple, 431 krb5_key_salt_tuple *ks_tuple, char ***ks_tuple_strs) 432{ 433 krb5_error_code rc = KRB5_PROG_ETYPE_NOSUPP; 434 char *ename, *sname; 435 char **ksnames; 436 int i; 437 438 *ks_tuple_strs = NULL; 439 if (n_ks_tuple < 1) 440 return 0; 441 442 if ((ksnames = calloc(n_ks_tuple, sizeof (*ksnames))) == NULL) 443 return (errno); 444 445 for (i = 0; i < n_ks_tuple; i++) { 446 if (krb5_enctype_to_string(context, ks_tuple[i].ks_enctype, &ename)) 447 goto out; 448 if (krb5_salttype_to_string(context, ks_tuple[i].ks_enctype, 449 ks_tuple[i].ks_salttype, &sname)) 450 goto out; 451 452 if (asprintf(&ksnames[i], "%s:%s", ename, sname) == -1) { 453 rc = errno; 454 free(ename); 455 free(sname); 456 goto out; 457 } 458 free(ename); 459 free(sname); 460 } 461 462 *ks_tuple_strs = ksnames; 463 rc = 0; 464 465out: 466 for (i = 0; i < n_ks_tuple; i++) 467 free(ksnames[i]); 468 free(ksnames); 469 return (rc); 470} 471 472/* 473 * Generate the `key_set' from the [kadmin]default_keys statement. If 474 * `no_salt' is set, salt is not important (and will not be set) since 475 * it's random keys that is going to be created. 476 */ 477 478krb5_error_code 479hdb_generate_key_set(krb5_context context, krb5_principal principal, 480 int n_ks_tuple, krb5_key_salt_tuple *ks_tuple, 481 Key **ret_key_set, size_t *nkeyset, int no_salt) 482{ 483 char **ktypes = NULL; 484 char **kp; 485 krb5_error_code ret; 486 Key *k, *key_set; 487 size_t i, j; 488 char **ks_tuple_strs; 489 static const char *default_keytypes[] = { 490 "aes256-cts-hmac-sha1-96:pw-salt", 491 "aes128-cts-hmac-sha1-96:pw-salt", 492 "des3-cbc-sha1:pw-salt", 493 NULL 494 }; 495 496 *ret_key_set = key_set = NULL; 497 *nkeyset = 0; 498 499 if ((ret = ks_tuple2str(context, n_ks_tuple, ks_tuple, &ks_tuple_strs))) 500 return ret; 501 502 if (ks_tuple_strs == NULL) 503 ktypes = krb5_config_get_strings(context, NULL, "kadmin", 504 "default_keys", NULL); 505 506 if (ktypes == NULL) 507 ktypes = (char **)(intptr_t)default_keytypes; 508 509 510 ret = 0; 511 512 for(kp = ktypes; kp && *kp; kp++) { 513 const char *p; 514 krb5_salt salt; 515 krb5_enctype *etypes; 516 size_t num_etypes; 517 518 p = *kp; 519 /* check alias */ 520 if(strcmp(p, "v5") == 0) 521 p = "pw-salt"; 522 else if(strcmp(p, "v4") == 0) 523 p = "des:pw-salt:"; 524 else if(strcmp(p, "afs") == 0 || strcmp(p, "afs3") == 0) 525 p = "des:afs3-salt"; 526 else if (strcmp(p, "arcfour-hmac-md5") == 0) 527 p = "arcfour-hmac-md5:pw-salt"; 528 529 memset(&salt, 0, sizeof(salt)); 530 531 ret = parse_key_set(context, p, 532 &etypes, &num_etypes, &salt, principal); 533 if (ret) { 534 krb5_warn(context, ret, "bad value for default_keys `%s'", *kp); 535 ret = 0; 536 continue; 537 } 538 539 for (i = 0; i < num_etypes; i++) { 540 /* find duplicates */ 541 for (j = 0; j < *nkeyset; j++) { 542 543 k = &key_set[j]; 544 545 if (k->key.keytype == etypes[i]) { 546 if (no_salt) 547 break; 548 if (k->salt == NULL && salt.salttype == KRB5_PW_SALT) 549 break; 550 if (k->salt->type == salt.salttype && 551 k->salt->salt.length == salt.saltvalue.length && 552 memcmp(k->salt->salt.data, salt.saltvalue.data, 553 salt.saltvalue.length) == 0) 554 break; 555 } 556 } 557 /* not a duplicate, lets add it */ 558 if (j == *nkeyset) { 559 ret = add_enctype_to_key_set(&key_set, nkeyset, etypes[i], 560 no_salt ? NULL : &salt); 561 if (ret) { 562 free(etypes); 563 krb5_free_salt(context, salt); 564 goto out; 565 } 566 } 567 } 568 free(etypes); 569 krb5_free_salt(context, salt); 570 } 571 572 *ret_key_set = key_set; 573 574 out: 575 if (ktypes != (char **)(intptr_t)default_keytypes) 576 krb5_config_free_strings(ktypes); 577 578 if (ret) { 579 krb5_warn(context, ret, 580 "failed to parse the [kadmin]default_keys values"); 581 582 for (i = 0; i < *nkeyset; i++) 583 free_Key(&key_set[i]); 584 free(key_set); 585 } else if (*nkeyset == 0) { 586 krb5_warnx(context, 587 "failed to parse any of the [kadmin]default_keys values"); 588 ret = EINVAL; /* XXX */ 589 } 590 591 return ret; 592} 593 594 595krb5_error_code 596hdb_generate_key_set_password(krb5_context context, 597 krb5_principal principal, 598 const char *password, 599 int n_ks_tuple, krb5_key_salt_tuple *ks_tuple, 600 Key **keys, size_t *num_keys) 601{ 602 krb5_error_code ret; 603 size_t i; 604 605 *keys = NULL; 606 *num_keys = 0; 607 608 ret = hdb_generate_key_set(context, principal, n_ks_tuple, ks_tuple, 609 keys, num_keys, 0); 610 if (ret) 611 return ret; 612 613 for (i = 0; i < (*num_keys); i++) { 614 krb5_salt salt; 615 616 if ((*keys)[i].salt) { 617 salt.salttype = (*keys)[i].salt->type; 618 salt.saltvalue.length = (*keys)[i].salt->salt.length; 619 salt.saltvalue.data = (*keys)[i].salt->salt.data; 620 } else { 621 memset(&salt, 0, sizeof(salt)); 622 } 623 624 ret = krb5_string_to_key_salt (context, 625 (*keys)[i].key.keytype, 626 password, 627 salt, 628 &(*keys)[i].key); 629 630 if(ret) 631 break; 632 } 633 634 if(ret) { 635 hdb_free_keys (context, *num_keys, *keys); 636 return ret; 637 } 638 return ret; 639} 640 641#include <corecrypto/ccsrp.h> 642 643krb5_error_code 644hdb_set_srp_verifier(krb5_context context, 645 KRB5_SRP_GROUP group, 646 krb5_const_principal principal, 647 const char *password, 648 uint32_t iterations, 649 hdb_srp *srp) 650{ 651 krb5_error_code ret; 652 const struct _krb5_srp_group *grp; 653 654 grp = _krb5_srp_validate_group(group); 655 if (grp == NULL) 656 return ENOENT; 657 658 /* 659 * Set up param 660 */ 661 662 srp->param.group = group; 663 ret = krb5_data_alloc(&srp->param.salt, 16); 664 if (ret) { 665 return ret; 666 } 667 668 krb5_generate_random_block(srp->param.salt.data, srp->param.salt.length); 669 670 if (iterations == 0) 671 iterations = 4000; 672 srp->param.iterations = iterations; 673 674 /* 675 * Now build verifier 676 */ 677 678 ret = _krb5_srp_create_pa(context, 679 grp, 680 principal, 681 password, 682 &srp->param, 683 &srp->verifier); 684 if (ret) { 685 free_hdb_srp(srp); 686 return ret; 687 } 688 689 return 0; 690} 691 692krb5_error_code 693hdb_entry_set_srp_verifiers(krb5_context context, 694 hdb_entry *entry, 695 const char *password, 696 uint32_t iterations) 697{ 698 HDB_extension ext; 699 int ret; 700 701 memset(&ext, 0, sizeof(ext)); 702 703 ext.mandatory = FALSE; 704 ext.data.element = choice_HDB_extension_data_srp; 705 706 ext.data.u.srp.len = 1; 707 ext.data.u.srp.val = calloc(1, sizeof(ext.data.u.srp.val[0])); 708 if (ext.data.u.srp.val == NULL) 709 return ENOMEM; 710 711 ret = hdb_set_srp_verifier(context, 712 KRB5_SRP_GROUP_RFC5054_4096_PBKDF2_SHA512, 713 entry->principal, 714 password, 715 0, 716 &ext.data.u.srp.val[0]); 717 if (ret) { 718 free_HDB_extension(&ext); 719 return ret; 720 } 721 722 ret = hdb_replace_extension(context, entry, &ext); 723 724 free_HDB_extension(&ext); 725 726 return ret; 727} 728