| 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 |
struct hx509_certs_data; |
| 37 |
struct krb5_pk_identity; |
| 38 |
struct krb5_pk_cert; |
| 39 |
struct ContentInfo; |
| 40 |
struct AlgorithmIdentifier; |
| 41 |
struct _krb5_krb_auth_data; |
| 42 |
typedef struct krb5_pk_init_ctx_data *krb5_pk_init_ctx; |
| 43 |
struct krb5_dh_moduli; |
| 44 |
struct _krb5_key_data; |
| 45 |
struct _krb5_encryption_type; |
| 46 |
struct _krb5_key_type; |
| 47 |
#include <pkinit_asn1.h> |
| 48 |
#include <krb5-private.h> |
| 49 |
#include <base64.h> |
| 50 |
|
| 51 |
/* |
| 52 |
* free all the memory used by (len, keys) |
| 53 |
*/ |
| 54 |
|
| 55 |
void |
| 56 |
hdb_free_keys(krb5_context context, int len, Key *keys) |
| 57 |
{ |
| 58 |
size_t i; |
| 59 |
|
| 60 |
for (i = 0; i < len; i++) { |
| 61 |
free(keys[i].mkvno); |
| 62 |
keys[i].mkvno = NULL; |
| 63 |
if (keys[i].salt != NULL) { |
| 64 |
free_Salt(keys[i].salt); |
| 65 |
free(keys[i].salt); |
| 66 |
keys[i].salt = NULL; |
| 67 |
} |
| 68 |
krb5_free_keyblock_contents(context, &keys[i].key); |
| 69 |
} |
| 70 |
free (keys); |
| 71 |
} |
| 72 |
|
| 73 |
/* |
| 74 |
* for each entry in `default_keys' try to parse it as a sequence |
| 75 |
* of etype:salttype:salt, syntax of this if something like: |
| 76 |
* [(des|des3|etype):](pw-salt|afs3)[:string], if etype is omitted it |
| 77 |
* means all etypes, and if string is omitted is means the default |
| 78 |
* string (for that principal). Additional special values: |
| 79 |
* v5 == pw-salt, and |
| 80 |
* v4 == des:pw-salt: |
| 81 |
* afs or afs3 == des:afs3-salt |
| 82 |
*/ |
| 83 |
|
| 84 |
static const krb5_enctype des_etypes[] = { |
| 85 |
KRB5_ENCTYPE_DES_CBC_MD5, |
| 86 |
KRB5_ENCTYPE_DES_CBC_MD4, |
| 87 |
KRB5_ENCTYPE_DES_CBC_CRC |
| 88 |
}; |
| 89 |
|
| 90 |
static const krb5_enctype all_etypes[] = { |
| 91 |
KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96, |
| 92 |
KRB5_ENCTYPE_DES3_CBC_SHA1, |
| 93 |
KRB5_ENCTYPE_ARCFOUR_HMAC_MD5 |
| 94 |
}; |
| 95 |
|
| 96 |
static krb5_error_code |
| 97 |
parse_key_set(krb5_context context, const char *key, |
| 98 |
krb5_enctype **ret_enctypes, size_t *ret_num_enctypes, |
| 99 |
krb5_salt *salt, krb5_principal principal) |
| 100 |
{ |
| 101 |
const char *p; |
| 102 |
char buf[3][256]; |
| 103 |
int num_buf = 0; |
| 104 |
int i, num_enctypes = 0; |
| 105 |
krb5_enctype e; |
| 106 |
const krb5_enctype *enctypes = NULL; |
| 107 |
krb5_error_code ret; |
| 108 |
|
| 109 |
p = key; |
| 110 |
|
| 111 |
*ret_enctypes = NULL; |
| 112 |
*ret_num_enctypes = 0; |
| 113 |
|
| 114 |
/* split p in a list of :-separated strings */ |
| 115 |
for(num_buf = 0; num_buf < 3; num_buf++) |
| 116 |
if(strsep_copy(&p, ":", buf[num_buf], sizeof(buf[num_buf])) == -1) |
| 117 |
break; |
| 118 |
|
| 119 |
salt->saltvalue.data = NULL; |
| 120 |
salt->saltvalue.length = 0; |
| 121 |
|
| 122 |
for(i = 0; i < num_buf; i++) { |
| 123 |
if(enctypes == NULL && num_buf > 1) { |
| 124 |
/* this might be a etype specifier */ |
| 125 |
/* XXX there should be a string_to_etypes handling |
| 126 |
special cases like `des' and `all' */ |
| 127 |
if(strcmp(buf[i], "des") == 0) { |
| 128 |
enctypes = des_etypes; |
| 129 |
num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]); |
| 130 |
} else if(strcmp(buf[i], "des3") == 0) { |
| 131 |
e = KRB5_ENCTYPE_DES3_CBC_SHA1; |
| 132 |
enctypes = &e; |
| 133 |
num_enctypes = 1; |
| 134 |
} else { |
| 135 |
ret = krb5_string_to_enctype(context, buf[i], &e); |
| 136 |
if (ret == 0) { |
| 137 |
enctypes = &e; |
| 138 |
num_enctypes = 1; |
| 139 |
} else |
| 140 |
return ret; |
| 141 |
} |
| 142 |
continue; |
| 143 |
} |
| 144 |
if(salt->salttype == 0) { |
| 145 |
/* interpret string as a salt specifier, if no etype |
| 146 |
is set, this sets default values */ |
| 147 |
/* XXX should perhaps use string_to_salttype, but that |
| 148 |
interface sucks */ |
| 149 |
if(strcmp(buf[i], "pw-salt") == 0) { |
| 150 |
if(enctypes == NULL) { |
| 151 |
enctypes = all_etypes; |
| 152 |
num_enctypes = sizeof(all_etypes)/sizeof(all_etypes[0]); |
| 153 |
} |
| 154 |
salt->salttype = KRB5_PW_SALT; |
| 155 |
} else if(strcmp(buf[i], "afs3-salt") == 0) { |
| 156 |
if(enctypes == NULL) { |
| 157 |
enctypes = des_etypes; |
| 158 |
num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]); |
| 159 |
} |
| 160 |
salt->salttype = KRB5_AFS3_SALT; |
| 161 |
} |
| 162 |
continue; |
| 163 |
} |
| 164 |
|
| 165 |
if (salt->saltvalue.data != NULL) |
| 166 |
free(salt->saltvalue.data); |
| 167 |
/* if there is a final string, use it as the string to |
| 168 |
salt with, this is mostly useful with null salt for |
| 169 |
v4 compat, and a cell name for afs compat */ |
| 170 |
salt->saltvalue.data = strdup(buf[i]); |
| 171 |
if (salt->saltvalue.data == NULL) |
| 172 |
return krb5_enomem(context); |
| 173 |
salt->saltvalue.length = strlen(buf[i]); |
| 174 |
} |
| 175 |
|
| 176 |
if(enctypes == NULL || salt->salttype == 0) { |
| 177 |
krb5_free_salt(context, *salt); |
| 178 |
krb5_set_error_message(context, EINVAL, "bad value for default_keys `%s'", key); |
| 179 |
return EINVAL; |
| 180 |
} |
| 181 |
|
| 182 |
/* if no salt was specified make up default salt */ |
| 183 |
if(salt->saltvalue.data == NULL) { |
| 184 |
if(salt->salttype == KRB5_PW_SALT) { |
| 185 |
ret = krb5_get_pw_salt(context, principal, salt); |
| 186 |
if (ret) |
| 187 |
return ret; |
| 188 |
} else if(salt->salttype == KRB5_AFS3_SALT) { |
| 189 |
krb5_const_realm realm = krb5_principal_get_realm(context, principal); |
| 190 |
salt->saltvalue.data = strdup(realm); |
| 191 |
if(salt->saltvalue.data == NULL) { |
| 192 |
krb5_set_error_message(context, ENOMEM, |
| 193 |
"out of memory while " |
| 194 |
"parsing salt specifiers"); |
| 195 |
return ENOMEM; |
| 196 |
} |
| 197 |
strlwr(salt->saltvalue.data); |
| 198 |
salt->saltvalue.length = strlen(realm); |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
*ret_enctypes = malloc(sizeof(enctypes[0]) * num_enctypes); |
| 203 |
if (*ret_enctypes == NULL) { |
| 204 |
krb5_free_salt(context, *salt); |
| 205 |
krb5_set_error_message(context, ENOMEM, "malloc: out of memory"); |
| 206 |
return ENOMEM; |
| 207 |
} |
| 208 |
memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * num_enctypes); |
| 209 |
*ret_num_enctypes = num_enctypes; |
| 210 |
|
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* This function prunes an HDB entry's keys that are too old to have been used |
| 216 |
* to mint still valid tickets (based on the entry's maximum ticket lifetime). |
| 217 |
* |
| 218 |
* @param context Context |
| 219 |
* @param entry HDB entry |
| 220 |
*/ |
| 221 |
krb5_error_code |
| 222 |
hdb_prune_keys(krb5_context context, hdb_entry *entry) |
| 223 |
{ |
| 224 |
HDB_extension *ext; |
| 225 |
HDB_Ext_KeySet *keys; |
| 226 |
size_t nelem; |
| 227 |
|
| 228 |
ext = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); |
| 229 |
if (ext == NULL) |
| 230 |
return 0; |
| 231 |
keys = &ext->data.u.hist_keys; |
| 232 |
nelem = keys->len; |
| 233 |
|
| 234 |
/* Optionally drop key history for keys older than now - max_life */ |
| 235 |
if (entry->max_life != NULL && nelem > 0 |
| 236 |
&& krb5_config_get_bool_default(context, NULL, FALSE, |
| 237 |
"kadmin", "prune-key-history", NULL)) { |
| 238 |
hdb_keyset *elem; |
| 239 |
time_t ceiling = time(NULL) - *entry->max_life; |
| 240 |
time_t keep_time = 0; |
| 241 |
size_t i; |
| 242 |
|
| 243 |
/* |
| 244 |
* Compute most recent key timestamp that predates the current time |
| 245 |
* by at least the entry's maximum ticket lifetime. |
| 246 |
*/ |
| 247 |
for (i = 0; i < nelem; ++i) { |
| 248 |
elem = &keys->val[i]; |
| 249 |
if (elem->set_time && *elem->set_time < ceiling |
| 250 |
&& (keep_time == 0 || *elem->set_time > keep_time)) |
| 251 |
keep_time = *elem->set_time; |
| 252 |
} |
| 253 |
|
| 254 |
/* Drop obsolete entries */ |
| 255 |
if (keep_time) { |
| 256 |
for (i = 0; i < nelem; /* see below */) { |
| 257 |
elem = &keys->val[i]; |
| 258 |
if (elem->set_time && *elem->set_time < keep_time) { |
| 259 |
remove_HDB_Ext_KeySet(keys, i); |
| 260 |
/* |
| 261 |
* Removing the i'th element shifts the tail down, continue |
| 262 |
* at same index with reduced upper bound. |
| 263 |
*/ |
| 264 |
--nelem; |
| 265 |
continue; |
| 266 |
} |
| 267 |
++i; |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
|
| 272 |
return 0; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* This function adds an HDB entry's current keyset to the entry's key |
| 277 |
* history. The current keyset is left alone; the caller is responsible |
| 278 |
* for freeing it. |
| 279 |
* |
| 280 |
* @param context Context |
| 281 |
* @param entry HDB entry |
| 282 |
*/ |
| 283 |
krb5_error_code |
| 284 |
hdb_add_current_keys_to_history(krb5_context context, hdb_entry *entry) |
| 285 |
{ |
| 286 |
krb5_boolean replace = FALSE; |
| 287 |
krb5_error_code ret; |
| 288 |
HDB_extension *ext; |
| 289 |
HDB_Ext_KeySet *keys; |
| 290 |
hdb_keyset newkeyset; |
| 291 |
time_t newtime; |
| 292 |
|
| 293 |
if (entry->keys.len == 0) |
| 294 |
return 0; /* nothing to do */ |
| 295 |
|
| 296 |
ext = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); |
| 297 |
if (ext == NULL) { |
| 298 |
replace = TRUE; |
| 299 |
ext = calloc(1, sizeof (*ext)); |
| 300 |
if (ext == NULL) |
| 301 |
return krb5_enomem(context); |
| 302 |
|
| 303 |
ext->data.element = choice_HDB_extension_data_hist_keys; |
| 304 |
} |
| 305 |
keys = &ext->data.u.hist_keys; |
| 306 |
|
| 307 |
ext->mandatory = FALSE; |
| 308 |
|
| 309 |
/* |
| 310 |
* Copy in newest old keyset |
| 311 |
*/ |
| 312 |
ret = hdb_entry_get_pw_change_time(entry, &newtime); |
| 313 |
if (ret) |
| 314 |
goto out; |
| 315 |
|
| 316 |
memset(&newkeyset, 0, sizeof(newkeyset)); |
| 317 |
newkeyset.keys = entry->keys; |
| 318 |
newkeyset.kvno = entry->kvno; |
| 319 |
newkeyset.set_time = &newtime; |
| 320 |
|
| 321 |
ret = add_HDB_Ext_KeySet(keys, &newkeyset); |
| 322 |
if (ret) |
| 323 |
goto out; |
| 324 |
|
| 325 |
if (replace) { |
| 326 |
/* hdb_replace_extension() deep-copies ext; what a waste */ |
| 327 |
ret = hdb_replace_extension(context, entry, ext); |
| 328 |
if (ret) |
| 329 |
goto out; |
| 330 |
} |
| 331 |
|
| 332 |
ret = hdb_prune_keys(context, entry); |
| 333 |
if (ret) |
| 334 |
goto out; |
| 335 |
|
| 336 |
out: |
| 337 |
if (replace && ext) { |
| 338 |
free_HDB_extension(ext); |
| 339 |
free(ext); |
| 340 |
} |
| 341 |
return ret; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* This function adds a key to an HDB entry's key history. |
| 346 |
* |
| 347 |
* @param context Context |
| 348 |
* @param entry HDB entry |
| 349 |
* @param kvno Key version number of the key to add to the history |
| 350 |
* @param key The Key to add |
| 351 |
*/ |
| 352 |
krb5_error_code |
| 353 |
hdb_add_history_key(krb5_context context, hdb_entry *entry, krb5_kvno kvno, Key *key) |
| 354 |
{ |
| 355 |
size_t i; |
| 356 |
hdb_keyset keyset; |
| 357 |
HDB_Ext_KeySet *hist_keys; |
| 358 |
HDB_extension ext; |
| 359 |
HDB_extension *extp; |
| 360 |
krb5_error_code ret; |
| 361 |
|
| 362 |
memset(&keyset, 0, sizeof (keyset)); |
| 363 |
memset(&ext, 0, sizeof (ext)); |
| 364 |
|
| 365 |
extp = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); |
| 366 |
if (extp == NULL) { |
| 367 |
ext.data.element = choice_HDB_extension_data_hist_keys; |
| 368 |
extp = &ext; |
| 369 |
} |
| 370 |
|
| 371 |
extp->mandatory = FALSE; |
| 372 |
hist_keys = &extp->data.u.hist_keys; |
| 373 |
|
| 374 |
for (i = 0; i < hist_keys->len; i++) { |
| 375 |
if (hist_keys->val[i].kvno == kvno) { |
| 376 |
ret = add_Keys(&hist_keys->val[i].keys, key); |
| 377 |
goto out; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
keyset.kvno = kvno; |
| 382 |
ret = add_Keys(&keyset.keys, key); |
| 383 |
if (ret) |
| 384 |
goto out; |
| 385 |
ret = add_HDB_Ext_KeySet(hist_keys, &keyset); |
| 386 |
if (ret) |
| 387 |
goto out; |
| 388 |
if (extp == &ext) { |
| 389 |
ret = hdb_replace_extension(context, entry, &ext); |
| 390 |
if (ret) |
| 391 |
goto out; |
| 392 |
} |
| 393 |
|
| 394 |
out: |
| 395 |
free_hdb_keyset(&keyset); |
| 396 |
free_HDB_extension(&ext); |
| 397 |
return ret; |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
/** |
| 402 |
* This function changes an hdb_entry's kvno, swapping the current key |
| 403 |
* set with a historical keyset. If no historical keys are found then |
| 404 |
* an error is returned (the caller can still set entry->kvno directly). |
| 405 |
* |
| 406 |
* @param context krb5_context |
| 407 |
* @param new_kvno New kvno for the entry |
| 408 |
* @param entry hdb_entry to modify |
| 409 |
*/ |
| 410 |
krb5_error_code |
| 411 |
hdb_change_kvno(krb5_context context, krb5_kvno new_kvno, hdb_entry *entry) |
| 412 |
{ |
| 413 |
HDB_extension ext; |
| 414 |
HDB_extension *extp; |
| 415 |
hdb_keyset keyset; |
| 416 |
HDB_Ext_KeySet *hist_keys; |
| 417 |
size_t i; |
| 418 |
int found = 0; |
| 419 |
krb5_error_code ret; |
| 420 |
|
| 421 |
if (entry->kvno == new_kvno) |
| 422 |
return 0; |
| 423 |
|
| 424 |
extp = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys); |
| 425 |
if (extp == NULL) { |
| 426 |
memset(&ext, 0, sizeof (ext)); |
| 427 |
ext.data.element = choice_HDB_extension_data_hist_keys; |
| 428 |
extp = &ext; |
| 429 |
} |
| 430 |
|
| 431 |
memset(&keyset, 0, sizeof (keyset)); |
| 432 |
hist_keys = &extp->data.u.hist_keys; |
| 433 |
for (i = 0; i < hist_keys->len; i++) { |
| 434 |
if (hist_keys->val[i].kvno == new_kvno) { |
| 435 |
found = 1; |
| 436 |
ret = copy_hdb_keyset(&hist_keys->val[i], &keyset); |
| 437 |
if (ret) |
| 438 |
goto out; |
| 439 |
ret = remove_HDB_Ext_KeySet(hist_keys, i); |
| 440 |
if (ret) |
| 441 |
goto out; |
| 442 |
break; |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
if (!found) |
| 447 |
return HDB_ERR_KVNO_NOT_FOUND; |
| 448 |
|
| 449 |
ret = hdb_add_current_keys_to_history(context, entry); |
| 450 |
if (ret) |
| 451 |
goto out; |
| 452 |
|
| 453 |
/* Note: we do nothing with keyset.set_time */ |
| 454 |
entry->kvno = new_kvno; |
| 455 |
entry->keys = keyset.keys; /* shortcut */ |
| 456 |
memset(&keyset.keys, 0, sizeof (keyset.keys)); |
| 457 |
|
| 458 |
out: |
| 459 |
free_hdb_keyset(&keyset); |
| 460 |
return ret; |
| 461 |
} |
| 462 |
|
| 463 |
|
| 464 |
static krb5_error_code |
| 465 |
add_enctype_to_key_set(Key **key_set, size_t *nkeyset, |
| 466 |
krb5_enctype enctype, krb5_salt *salt) |
| 467 |
{ |
| 468 |
krb5_error_code ret; |
| 469 |
Key key, *tmp; |
| 470 |
|
| 471 |
memset(&key, 0, sizeof(key)); |
| 472 |
|
| 473 |
tmp = realloc(*key_set, (*nkeyset + 1) * sizeof((*key_set)[0])); |
| 474 |
if (tmp == NULL) |
| 475 |
return ENOMEM; |
| 476 |
|
| 477 |
*key_set = tmp; |
| 478 |
|
| 479 |
key.key.keytype = enctype; |
| 480 |
key.key.keyvalue.length = 0; |
| 481 |
key.key.keyvalue.data = NULL; |
| 482 |
|
| 483 |
if (salt) { |
| 484 |
key.salt = calloc(1, sizeof(*key.salt)); |
| 485 |
if (key.salt == NULL) { |
| 486 |
free_Key(&key); |
| 487 |
return ENOMEM; |
| 488 |
} |
| 489 |
|
| 490 |
key.salt->type = salt->salttype; |
| 491 |
krb5_data_zero (&key.salt->salt); |
| 492 |
|
| 493 |
ret = krb5_data_copy(&key.salt->salt, |
| 494 |
salt->saltvalue.data, |
| 495 |
salt->saltvalue.length); |
| 496 |
if (ret) { |
| 497 |
free_Key(&key); |
| 498 |
return ret; |
| 499 |
} |
| 500 |
} else |
| 501 |
key.salt = NULL; |
| 502 |
|
| 503 |
(*key_set)[*nkeyset] = key; |
| 504 |
|
| 505 |
*nkeyset += 1; |
| 506 |
|
| 507 |
return 0; |
| 508 |
} |
| 509 |
|
| 510 |
|
| 511 |
static |
| 512 |
krb5_error_code |
| 513 |
ks_tuple2str(krb5_context context, int n_ks_tuple, |
| 514 |
krb5_key_salt_tuple *ks_tuple, char ***ks_tuple_strs) |
| 515 |
{ |
| 516 |
size_t i; |
| 517 |
char **ksnames; |
| 518 |
krb5_error_code rc = KRB5_PROG_ETYPE_NOSUPP; |
| 519 |
|
| 520 |
*ks_tuple_strs = NULL; |
| 521 |
if (n_ks_tuple < 1) |
| 522 |
return 0; |
| 523 |
|
| 524 |
if ((ksnames = calloc(n_ks_tuple + 1, sizeof (*ksnames))) == NULL) |
| 525 |
return (errno); |
| 526 |
|
| 527 |
for (i = 0; i < n_ks_tuple; i++) { |
| 528 |
char *ename, *sname; |
| 529 |
|
| 530 |
if (krb5_enctype_to_string(context, ks_tuple[i].ks_enctype, &ename)) |
| 531 |
goto out; |
| 532 |
if (krb5_salttype_to_string(context, ks_tuple[i].ks_enctype, |
| 533 |
ks_tuple[i].ks_salttype, &sname)) { |
| 534 |
free(ename); |
| 535 |
goto out; |
| 536 |
} |
| 537 |
|
| 538 |
if (asprintf(&ksnames[i], "%s:%s", ename, sname) == -1) { |
| 539 |
rc = errno; |
| 540 |
free(ename); |
| 541 |
free(sname); |
| 542 |
goto out; |
| 543 |
} |
| 544 |
free(ename); |
| 545 |
free(sname); |
| 546 |
} |
| 547 |
|
| 548 |
ksnames[i] = NULL; |
| 549 |
*ks_tuple_strs = ksnames; |
| 550 |
return 0; |
| 551 |
|
| 552 |
out: |
| 553 |
for (i = 0; i < n_ks_tuple; i++) |
| 554 |
free(ksnames[i]); |
| 555 |
free(ksnames); |
| 556 |
return (rc); |
| 557 |
} |
| 558 |
|
| 559 |
/* |
| 560 |
* |
| 561 |
*/ |
| 562 |
|
| 563 |
static char ** |
| 564 |
glob_rules_keys(krb5_context context, krb5_const_principal principal) |
| 565 |
{ |
| 566 |
const krb5_config_binding *list; |
| 567 |
krb5_principal pattern; |
| 568 |
krb5_error_code ret; |
| 569 |
|
| 570 |
list = krb5_config_get_list(context, NULL, "kadmin", |
| 571 |
"default_key_rules", NULL); |
| 572 |
if (list == NULL) |
| 573 |
return NULL; |
| 574 |
|
| 575 |
while (list) { |
| 576 |
if (list->type == krb5_config_string) { |
| 577 |
ret = krb5_parse_name(context, list->name, &pattern); |
| 578 |
if (ret == 0) { |
| 579 |
ret = krb5_principal_match(context, principal, pattern); |
| 580 |
krb5_free_principal(context, pattern); |
| 581 |
if (ret) { |
| 582 |
return krb5_config_get_strings(context, list, |
| 583 |
list->name, NULL); |
| 584 |
} |
| 585 |
} |
| 586 |
} |
| 587 |
list = list->next; |
| 588 |
} |
| 589 |
return NULL; |
| 590 |
} |
| 591 |
|
| 592 |
/* |
| 593 |
* NIST guidance in Section 5.1 of [SP800-132] requires that a portion |
| 594 |
* of the salt of at least 128 bits shall be randomly generated. |
| 595 |
*/ |
| 596 |
static krb5_error_code |
| 597 |
add_random_to_salt(krb5_context context, krb5_salt *in, krb5_salt *out) |
| 598 |
{ |
| 599 |
krb5_error_code ret; |
| 600 |
char *p; |
| 601 |
unsigned char random[16]; |
| 602 |
char *s; |
| 603 |
int slen; |
| 604 |
|
| 605 |
krb5_generate_random_block(random, sizeof(random)); |
| 606 |
|
| 607 |
slen = rk_base64_encode(random, sizeof(random), &s); |
| 608 |
if (slen < 0) |
| 609 |
return ENOMEM; |
| 610 |
|
| 611 |
ret = krb5_data_alloc(&out->saltvalue, slen + in->saltvalue.length); |
| 612 |
if (ret) { |
| 613 |
free(s); |
| 614 |
return ret; |
| 615 |
} |
| 616 |
|
| 617 |
p = out->saltvalue.data; |
| 618 |
memcpy(p, s, slen); |
| 619 |
memcpy(&p[slen], in->saltvalue.data, in->saltvalue.length); |
| 620 |
|
| 621 |
out->salttype = in->salttype; |
| 622 |
free(s); |
| 623 |
|
| 624 |
return 0; |
| 625 |
} |
| 626 |
|
| 627 |
/* |
| 628 |
* Generate the `key_set' from the [kadmin]default_keys statement. If |
| 629 |
* `no_salt' is set, salt is not important (and will not be set) since |
| 630 |
* it's random keys that is going to be created. |
| 631 |
*/ |
| 632 |
|
| 633 |
krb5_error_code |
| 634 |
hdb_generate_key_set(krb5_context context, krb5_principal principal, |
| 635 |
krb5_key_salt_tuple *ks_tuple, int n_ks_tuple, |
| 636 |
Key **ret_key_set, size_t *nkeyset, int no_salt) |
| 637 |
{ |
| 638 |
char **ktypes = NULL; |
| 639 |
char **kp; |
| 640 |
krb5_error_code ret; |
| 641 |
Key *k, *key_set; |
| 642 |
size_t i, j; |
| 643 |
char **ks_tuple_strs; |
| 644 |
char **config_ktypes = NULL; |
| 645 |
static const char *default_keytypes[] = { |
| 646 |
"aes256-cts-hmac-sha1-96:pw-salt", |
| 647 |
"des3-cbc-sha1:pw-salt", |
| 648 |
"arcfour-hmac-md5:pw-salt", |
| 649 |
NULL |
| 650 |
}; |
| 651 |
|
| 652 |
if ((ret = ks_tuple2str(context, n_ks_tuple, ks_tuple, &ks_tuple_strs))) |
| 653 |
return ret; |
| 654 |
|
| 655 |
ktypes = ks_tuple_strs; |
| 656 |
if (ktypes == NULL) { |
| 657 |
ktypes = glob_rules_keys(context, principal); |
| 658 |
} |
| 659 |
if (ktypes == NULL) { |
| 660 |
config_ktypes = krb5_config_get_strings(context, NULL, "kadmin", |
| 661 |
"default_keys", NULL); |
| 662 |
ktypes = config_ktypes; |
| 663 |
} |
| 664 |
if (ktypes == NULL) |
| 665 |
ktypes = (char **)(intptr_t)default_keytypes; |
| 666 |
|
| 667 |
*ret_key_set = key_set = NULL; |
| 668 |
*nkeyset = 0; |
| 669 |
|
| 670 |
for(kp = ktypes; kp && *kp; kp++) { |
| 671 |
const char *p; |
| 672 |
krb5_salt salt; |
| 673 |
krb5_enctype *enctypes; |
| 674 |
size_t num_enctypes; |
| 675 |
|
| 676 |
p = *kp; |
| 677 |
/* check alias */ |
| 678 |
if(strcmp(p, "v5") == 0) |
| 679 |
p = "pw-salt"; |
| 680 |
else if(strcmp(p, "v4") == 0) |
| 681 |
p = "des:pw-salt:"; |
| 682 |
else if(strcmp(p, "afs") == 0 || strcmp(p, "afs3") == 0) |
| 683 |
p = "des:afs3-salt"; |
| 684 |
else if (strcmp(p, "arcfour-hmac-md5") == 0) |
| 685 |
p = "arcfour-hmac-md5:pw-salt"; |
| 686 |
|
| 687 |
memset(&salt, 0, sizeof(salt)); |
| 688 |
|
| 689 |
ret = parse_key_set(context, p, |
| 690 |
&enctypes, &num_enctypes, &salt, principal); |
| 691 |
if (ret) { |
| 692 |
krb5_warn(context, ret, "bad value for default_keys `%s'", *kp); |
| 693 |
ret = 0; |
| 694 |
krb5_free_salt(context, salt); |
| 695 |
continue; |
| 696 |
} |
| 697 |
|
| 698 |
for (i = 0; i < num_enctypes; i++) { |
| 699 |
krb5_salt *saltp = no_salt ? NULL : &salt; |
| 700 |
krb5_salt rsalt; |
| 701 |
|
| 702 |
/* find duplicates */ |
| 703 |
for (j = 0; j < *nkeyset; j++) { |
| 704 |
|
| 705 |
k = &key_set[j]; |
| 706 |
|
| 707 |
if (k->key.keytype == enctypes[i]) { |
| 708 |
if (no_salt) |
| 709 |
break; |
| 710 |
if (k->salt == NULL && salt.salttype == KRB5_PW_SALT) |
| 711 |
break; |
| 712 |
if (k->salt->type == salt.salttype && |
| 713 |
k->salt->salt.length == salt.saltvalue.length && |
| 714 |
memcmp(k->salt->salt.data, salt.saltvalue.data, |
| 715 |
salt.saltvalue.length) == 0) |
| 716 |
break; |
| 717 |
} |
| 718 |
} |
| 719 |
/* not a duplicate, lets add it */ |
| 720 |
if (j < *nkeyset) |
| 721 |
continue; |
| 722 |
|
| 723 |
memset(&rsalt, 0, sizeof(rsalt)); |
| 724 |
|
| 725 |
/* prepend salt with randomness if required */ |
| 726 |
if (!no_salt && |
| 727 |
_krb5_enctype_requires_random_salt(context, enctypes[i])) { |
| 728 |
saltp = &rsalt; |
| 729 |
ret = add_random_to_salt(context, &salt, &rsalt); |
| 730 |
} |
| 731 |
|
| 732 |
if (ret == 0) |
| 733 |
ret = add_enctype_to_key_set(&key_set, nkeyset, enctypes[i], |
| 734 |
saltp); |
| 735 |
krb5_free_salt(context, rsalt); |
| 736 |
|
| 737 |
if (ret) { |
| 738 |
free(enctypes); |
| 739 |
krb5_free_salt(context, salt); |
| 740 |
goto out; |
| 741 |
} |
| 742 |
} |
| 743 |
free(enctypes); |
| 744 |
krb5_free_salt(context, salt); |
| 745 |
} |
| 746 |
|
| 747 |
*ret_key_set = key_set; |
| 748 |
|
| 749 |
out: |
| 750 |
if (config_ktypes != NULL) |
| 751 |
krb5_config_free_strings(config_ktypes); |
| 752 |
|
| 753 |
for(kp = ks_tuple_strs; kp && *kp; kp++) |
| 754 |
free(*kp); |
| 755 |
free(ks_tuple_strs); |
| 756 |
|
| 757 |
if (ret) { |
| 758 |
krb5_warn(context, ret, |
| 759 |
"failed to parse the [kadmin]default_keys values"); |
| 760 |
|
| 761 |
for (i = 0; i < *nkeyset; i++) |
| 762 |
free_Key(&key_set[i]); |
| 763 |
free(key_set); |
| 764 |
} else if (*nkeyset == 0) { |
| 765 |
krb5_warnx(context, |
| 766 |
"failed to parse any of the [kadmin]default_keys values"); |
| 767 |
ret = EINVAL; /* XXX */ |
| 768 |
} |
| 769 |
|
| 770 |
return ret; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
krb5_error_code |
| 775 |
hdb_generate_key_set_password_with_ks_tuple(krb5_context context, |
| 776 |
krb5_principal principal, |
| 777 |
const char *password, |
| 778 |
krb5_key_salt_tuple *ks_tuple, |
| 779 |
int n_ks_tuple, |
| 780 |
Key **keys, size_t *num_keys) |
| 781 |
{ |
| 782 |
krb5_error_code ret; |
| 783 |
size_t i; |
| 784 |
|
| 785 |
ret = hdb_generate_key_set(context, principal, ks_tuple, n_ks_tuple, |
| 786 |
keys, num_keys, 0); |
| 787 |
if (ret) |
| 788 |
return ret; |
| 789 |
|
| 790 |
for (i = 0; i < (*num_keys); i++) { |
| 791 |
krb5_salt salt; |
| 792 |
Key *key = &(*keys)[i]; |
| 793 |
|
| 794 |
salt.salttype = key->salt->type; |
| 795 |
salt.saltvalue.length = key->salt->salt.length; |
| 796 |
salt.saltvalue.data = key->salt->salt.data; |
| 797 |
|
| 798 |
ret = krb5_string_to_key_salt (context, |
| 799 |
key->key.keytype, |
| 800 |
password, |
| 801 |
salt, |
| 802 |
&key->key); |
| 803 |
if(ret) |
| 804 |
break; |
| 805 |
} |
| 806 |
|
| 807 |
if(ret) { |
| 808 |
hdb_free_keys (context, *num_keys, *keys); |
| 809 |
return ret; |
| 810 |
} |
| 811 |
return ret; |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
krb5_error_code |
| 816 |
hdb_generate_key_set_password(krb5_context context, |
| 817 |
krb5_principal principal, |
| 818 |
const char *password, |
| 819 |
Key **keys, size_t *num_keys) |
| 820 |
{ |
| 821 |
|
| 822 |
return hdb_generate_key_set_password_with_ks_tuple(context, principal, |
| 823 |
password, NULL, 0, |
| 824 |
keys, num_keys); |
| 825 |
} |