clean files
[heimdal.git] / lib / hdb / keys.c
bloba82407ad86dfe865838c92651da377eb1e8bf204
2 /*
3 * Copyright (c) 1997 - 2011 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the Institute nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "hdb_locl.h"
38 * free all the memory used by (len, keys)
41 void
42 hdb_free_keys(krb5_context context, int len, Key *keys)
44 size_t i;
46 for (i = 0; i < len; i++) {
47 free(keys[i].mkvno);
48 keys[i].mkvno = NULL;
49 if (keys[i].salt != NULL) {
50 free_Salt(keys[i].salt);
51 free(keys[i].salt);
52 keys[i].salt = NULL;
54 krb5_free_keyblock_contents(context, &keys[i].key);
56 free (keys);
60 * for each entry in `default_keys' try to parse it as a sequence
61 * of etype:salttype:salt, syntax of this if something like:
62 * [(des|des3|etype):](pw-salt|afs3)[:string], if etype is omitted it
63 * means all etypes, and if string is omitted is means the default
64 * string (for that principal). Additional special values:
65 * v5 == pw-salt, and
66 * v4 == des:pw-salt:
67 * afs or afs3 == des:afs3-salt
70 static const krb5_enctype des_etypes[] = {
71 KRB5_ENCTYPE_DES_CBC_MD5,
72 KRB5_ENCTYPE_DES_CBC_MD4,
73 KRB5_ENCTYPE_DES_CBC_CRC
76 static const krb5_enctype all_etypes[] = {
77 KRB5_ENCTYPE_AES256_CTS_HMAC_SHA1_96,
78 KRB5_ENCTYPE_ARCFOUR_HMAC_MD5,
79 KRB5_ENCTYPE_DES3_CBC_SHA1
82 static krb5_error_code
83 parse_key_set(krb5_context context, const char *key,
84 krb5_enctype **ret_enctypes, size_t *ret_num_enctypes,
85 krb5_salt *salt, krb5_principal principal)
87 const char *p;
88 char buf[3][256];
89 int num_buf = 0;
90 int i, num_enctypes = 0;
91 krb5_enctype e;
92 const krb5_enctype *enctypes = NULL;
93 krb5_error_code ret;
95 p = key;
97 *ret_enctypes = NULL;
98 *ret_num_enctypes = 0;
100 /* split p in a list of :-separated strings */
101 for(num_buf = 0; num_buf < 3; num_buf++)
102 if(strsep_copy(&p, ":", buf[num_buf], sizeof(buf[num_buf])) == -1)
103 break;
105 salt->saltvalue.data = NULL;
106 salt->saltvalue.length = 0;
108 for(i = 0; i < num_buf; i++) {
109 if(enctypes == NULL && num_buf > 1) {
110 /* this might be a etype specifier */
111 /* XXX there should be a string_to_etypes handling
112 special cases like `des' and `all' */
113 if(strcmp(buf[i], "des") == 0) {
114 enctypes = des_etypes;
115 num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]);
116 } else if(strcmp(buf[i], "des3") == 0) {
117 e = KRB5_ENCTYPE_DES3_CBC_SHA1;
118 enctypes = &e;
119 num_enctypes = 1;
120 } else {
121 ret = krb5_string_to_enctype(context, buf[i], &e);
122 if (ret == 0) {
123 enctypes = &e;
124 num_enctypes = 1;
125 } else
126 return ret;
128 continue;
130 if(salt->salttype == 0) {
131 /* interpret string as a salt specifier, if no etype
132 is set, this sets default values */
133 /* XXX should perhaps use string_to_salttype, but that
134 interface sucks */
135 if(strcmp(buf[i], "pw-salt") == 0) {
136 if(enctypes == NULL) {
137 enctypes = all_etypes;
138 num_enctypes = sizeof(all_etypes)/sizeof(all_etypes[0]);
140 salt->salttype = KRB5_PW_SALT;
141 } else if(strcmp(buf[i], "afs3-salt") == 0) {
142 if(enctypes == NULL) {
143 enctypes = des_etypes;
144 num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]);
146 salt->salttype = KRB5_AFS3_SALT;
148 continue;
152 /* if there is a final string, use it as the string to
153 salt with, this is mostly useful with null salt for
154 v4 compat, and a cell name for afs compat */
155 salt->saltvalue.data = strdup(buf[i]);
156 if (salt->saltvalue.data == NULL) {
157 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
158 return ENOMEM;
160 salt->saltvalue.length = strlen(buf[i]);
164 if(enctypes == NULL || salt->salttype == 0) {
165 krb5_set_error_message(context, EINVAL, "bad value for default_keys `%s'", key);
166 return EINVAL;
169 /* if no salt was specified make up default salt */
170 if(salt->saltvalue.data == NULL) {
171 if(salt->salttype == KRB5_PW_SALT)
172 ret = krb5_get_pw_salt(context, principal, salt);
173 else if(salt->salttype == KRB5_AFS3_SALT) {
174 krb5_const_realm realm = krb5_principal_get_realm(context, principal);
175 salt->saltvalue.data = strdup(realm);
176 if(salt->saltvalue.data == NULL) {
177 krb5_set_error_message(context, ENOMEM,
178 "out of memory while "
179 "parsing salt specifiers");
180 return ENOMEM;
182 strlwr(salt->saltvalue.data);
183 salt->saltvalue.length = strlen(realm);
187 *ret_enctypes = malloc(sizeof(enctypes[0]) * num_enctypes);
188 if (*ret_enctypes == NULL) {
189 krb5_free_salt(context, *salt);
190 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
191 return ENOMEM;
193 memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * num_enctypes);
194 *ret_num_enctypes = num_enctypes;
196 return 0;
201 * This function adds an HDB entry's current keyset to the entry's key
202 * history. The current keyset is left alone; the caller is responsible
203 * for freeing it.
205 * @param context Context
206 * @param entry HDB entry
208 krb5_error_code
209 hdb_add_current_keys_to_history(krb5_context context, hdb_entry *entry)
211 krb5_boolean replace = FALSE;
212 krb5_error_code ret;
213 HDB_extension *ext;
214 hdb_keyset newkeyset;
215 time_t newtime;
217 if (entry->keys.len == 0)
218 return 0; /* nothing to do */
220 ext = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys);
221 if (ext == NULL) {
222 replace = TRUE;
223 ext = calloc(1, sizeof (*ext));
224 if (ext == NULL)
225 return krb5_enomem(context);
227 ext->data.element = choice_HDB_extension_data_hist_keys;
231 * Copy in newest old keyset
233 ret = hdb_entry_get_pw_change_time(entry, &newtime);
234 if (ret)
235 goto out;
237 memset(&newkeyset, 0, sizeof(newkeyset));
238 newkeyset.keys = entry->keys;
239 newkeyset.kvno = entry->kvno;
240 newkeyset.set_time = &newtime;
242 ret = add_HDB_Ext_KeySet(&ext->data.u.hist_keys, &newkeyset);
243 if (ret)
244 goto out;
246 if (replace) {
247 /* hdb_replace_extension() deep-copies ext; what a waste */
248 ret = hdb_replace_extension(context, entry, ext);
249 if (ret)
250 goto out;
253 out:
254 if (replace && ext) {
255 free_HDB_extension(ext);
256 free(ext);
258 return ret;
262 * This function adds a key to an HDB entry's key history.
264 * @param context Context
265 * @param entry HDB entry
266 * @param kvno Key version number of the key to add to the history
267 * @param key The Key to add
269 krb5_error_code
270 hdb_add_history_key(krb5_context context, hdb_entry *entry, krb5_kvno kvno, Key *key)
272 size_t i;
273 hdb_keyset keyset;
274 HDB_Ext_KeySet *hist_keys;
275 HDB_extension ext;
276 HDB_extension *extp;
277 krb5_error_code ret;
279 memset(&keyset, 0, sizeof (keyset));
280 memset(&ext, 0, sizeof (ext));
282 extp = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys);
283 if (extp == NULL) {
284 ext.data.element = choice_HDB_extension_data_hist_keys;
285 extp = &ext;
288 hist_keys = &extp->data.u.hist_keys;
290 for (i = 0; i < hist_keys->len; i++) {
291 if (hist_keys->val[i].kvno == kvno) {
292 ret = add_Keys(&hist_keys->val[i].keys, key);
293 goto out;
297 keyset.kvno = kvno;
298 ret = add_Keys(&keyset.keys, key);
299 if (ret)
300 goto out;
301 ret = add_HDB_Ext_KeySet(hist_keys, &keyset);
302 if (ret)
303 goto out;
304 if (extp == &ext) {
305 ret = hdb_replace_extension(context, entry, &ext);
306 if (ret)
307 goto out;
310 out:
311 free_hdb_keyset(&keyset);
312 free_HDB_extension(&ext);
313 return ret;
318 * This function changes an hdb_entry's kvno, swapping the current key
319 * set with a historical keyset. If no historical keys are found then
320 * an error is returned (the caller can still set entry->kvno directly).
322 * @param context krb5_context
323 * @param new_kvno New kvno for the entry
324 * @param entry hdb_entry to modify
326 krb5_error_code
327 hdb_change_kvno(krb5_context context, krb5_kvno new_kvno, hdb_entry *entry)
329 HDB_extension ext;
330 HDB_extension *extp;
331 hdb_keyset keyset;
332 HDB_Ext_KeySet *hist_keys;
333 size_t i;
334 int found = 0;
335 krb5_error_code ret;
337 if (entry->kvno == new_kvno)
338 return 0;
340 extp = hdb_find_extension(entry, choice_HDB_extension_data_hist_keys);
341 if (extp == NULL) {
342 memset(&ext, 0, sizeof (ext));
343 ext.data.element = choice_HDB_extension_data_hist_keys;
344 extp = &ext;
347 memset(&keyset, 0, sizeof (keyset));
348 hist_keys = &extp->data.u.hist_keys;
349 for (i = 0; i < hist_keys->len; i++) {
350 if (hist_keys->val[i].kvno == new_kvno) {
351 found = 1;
352 ret = copy_hdb_keyset(&hist_keys->val[i], &keyset);
353 if (ret)
354 goto out;
355 ret = remove_HDB_Ext_KeySet(hist_keys, i);
356 if (ret)
357 goto out;
358 break;
362 if (!found)
363 return HDB_ERR_KVNO_NOT_FOUND;
365 ret = hdb_add_current_keys_to_history(context, entry);
366 if (ret)
367 goto out;
369 /* Note: we do nothing with keyset.set_time */
370 entry->kvno = new_kvno;
371 entry->keys = keyset.keys; /* shortcut */
372 memset(&keyset.keys, 0, sizeof (keyset.keys));
374 out:
375 free_hdb_keyset(&keyset);
376 return ret;
380 static krb5_error_code
381 add_enctype_to_key_set(Key **key_set, size_t *nkeyset,
382 krb5_enctype enctype, krb5_salt *salt)
384 krb5_error_code ret;
385 Key key, *tmp;
387 memset(&key, 0, sizeof(key));
389 tmp = realloc(*key_set, (*nkeyset + 1) * sizeof((*key_set)[0]));
390 if (tmp == NULL)
391 return ENOMEM;
393 *key_set = tmp;
395 key.key.keytype = enctype;
396 key.key.keyvalue.length = 0;
397 key.key.keyvalue.data = NULL;
399 if (salt) {
400 key.salt = calloc(1, sizeof(*key.salt));
401 if (key.salt == NULL) {
402 free_Key(&key);
403 return ENOMEM;
406 key.salt->type = salt->salttype;
407 krb5_data_zero (&key.salt->salt);
409 ret = krb5_data_copy(&key.salt->salt,
410 salt->saltvalue.data,
411 salt->saltvalue.length);
412 if (ret) {
413 free_Key(&key);
414 return ret;
416 } else
417 key.salt = NULL;
419 (*key_set)[*nkeyset] = key;
421 *nkeyset += 1;
423 return 0;
427 static
428 krb5_error_code
429 ks_tuple2str(krb5_context context, int n_ks_tuple,
430 krb5_key_salt_tuple *ks_tuple, char ***ks_tuple_strs)
432 size_t i;
433 char **ksnames;
434 char *ename, *sname;
435 krb5_error_code rc = KRB5_PROG_ETYPE_NOSUPP;
437 *ks_tuple_strs = NULL;
438 if (n_ks_tuple < 1)
439 return 0;
441 if ((ksnames = calloc(n_ks_tuple + 1, sizeof (*ksnames))) == NULL)
442 return (errno);
444 for (i = 0; i < n_ks_tuple; i++) {
445 if (krb5_enctype_to_string(context, ks_tuple[i].ks_enctype, &ename))
446 goto out;
447 if (krb5_salttype_to_string(context, ks_tuple[i].ks_enctype,
448 ks_tuple[i].ks_salttype, &sname))
449 goto out;
451 if (asprintf(&ksnames[i], "%s:%s", ename, sname) == -1) {
452 rc = errno;
453 free(ename);
454 free(sname);
455 goto out;
457 free(ename);
458 free(sname);
461 ksnames[i] = NULL;
462 *ks_tuple_strs = ksnames;
463 return 0;
465 out:
466 for (i = 0; i < n_ks_tuple; i++)
467 free(ksnames[i]);
468 free(ksnames);
469 return (rc);
476 static char **
477 glob_rules_keys(krb5_context context, krb5_const_principal principal)
479 const krb5_config_binding *list;
480 krb5_principal pattern;
481 krb5_error_code ret;
483 list = krb5_config_get_list(context, NULL, "kadmin",
484 "default_key_rules", NULL);
485 if (list == NULL)
486 return NULL;
488 while (list) {
489 if (list->type == krb5_config_string) {
490 ret = krb5_parse_name(context, list->name, &pattern);
491 if (ret == 0) {
492 ret = krb5_principal_match(context, principal, pattern);
493 krb5_free_principal(context, pattern);
494 if (ret) {
495 return krb5_config_get_strings(context, list,
496 list->name, NULL);
500 list = list->next;
502 return NULL;
506 * Generate the `key_set' from the [kadmin]default_keys statement. If
507 * `no_salt' is set, salt is not important (and will not be set) since
508 * it's random keys that is going to be created.
511 krb5_error_code
512 hdb_generate_key_set(krb5_context context, krb5_principal principal,
513 krb5_key_salt_tuple *ks_tuple, int n_ks_tuple,
514 Key **ret_key_set, size_t *nkeyset, int no_salt)
516 char **ktypes = NULL;
517 char **kp;
518 krb5_error_code ret;
519 Key *k, *key_set;
520 size_t i, j;
521 char **ks_tuple_strs;
522 char **config_ktypes = NULL;
523 static const char *default_keytypes[] = {
524 "aes256-cts-hmac-sha1-96:pw-salt",
525 "des3-cbc-sha1:pw-salt",
526 "arcfour-hmac-md5:pw-salt",
527 NULL
530 if ((ret = ks_tuple2str(context, n_ks_tuple, ks_tuple, &ks_tuple_strs)))
531 return ret;
533 ktypes = ks_tuple_strs;
534 if (ktypes == NULL) {
535 ktypes = glob_rules_keys(context, principal);
537 if (ktypes == NULL) {
538 config_ktypes = krb5_config_get_strings(context, NULL, "kadmin",
539 "default_keys", NULL);
540 ktypes = config_ktypes;
542 if (ktypes == NULL)
543 ktypes = (char **)(intptr_t)default_keytypes;
545 *ret_key_set = key_set = NULL;
546 *nkeyset = 0;
548 for(kp = ktypes; kp && *kp; kp++) {
549 const char *p;
550 krb5_salt salt;
551 krb5_enctype *enctypes;
552 size_t num_enctypes;
554 p = *kp;
555 /* check alias */
556 if(strcmp(p, "v5") == 0)
557 p = "pw-salt";
558 else if(strcmp(p, "v4") == 0)
559 p = "des:pw-salt:";
560 else if(strcmp(p, "afs") == 0 || strcmp(p, "afs3") == 0)
561 p = "des:afs3-salt";
562 else if (strcmp(p, "arcfour-hmac-md5") == 0)
563 p = "arcfour-hmac-md5:pw-salt";
565 memset(&salt, 0, sizeof(salt));
567 ret = parse_key_set(context, p,
568 &enctypes, &num_enctypes, &salt, principal);
569 if (ret) {
570 krb5_warn(context, ret, "bad value for default_keys `%s'", *kp);
571 ret = 0;
572 continue;
575 for (i = 0; i < num_enctypes; i++) {
576 /* find duplicates */
577 for (j = 0; j < *nkeyset; j++) {
579 k = &key_set[j];
581 if (k->key.keytype == enctypes[i]) {
582 if (no_salt)
583 break;
584 if (k->salt == NULL && salt.salttype == KRB5_PW_SALT)
585 break;
586 if (k->salt->type == salt.salttype &&
587 k->salt->salt.length == salt.saltvalue.length &&
588 memcmp(k->salt->salt.data, salt.saltvalue.data,
589 salt.saltvalue.length) == 0)
590 break;
593 /* not a duplicate, lets add it */
594 if (j == *nkeyset) {
595 ret = add_enctype_to_key_set(&key_set, nkeyset, enctypes[i],
596 no_salt ? NULL : &salt);
597 if (ret) {
598 free(enctypes);
599 krb5_free_salt(context, salt);
600 goto out;
604 free(enctypes);
605 krb5_free_salt(context, salt);
608 *ret_key_set = key_set;
610 out:
611 if (config_ktypes != NULL)
612 krb5_config_free_strings(config_ktypes);
614 for(kp = ks_tuple_strs; kp && *kp; kp++)
615 free(*kp);
616 free(ks_tuple_strs);
618 if (ret) {
619 krb5_warn(context, ret,
620 "failed to parse the [kadmin]default_keys values");
622 for (i = 0; i < *nkeyset; i++)
623 free_Key(&key_set[i]);
624 free(key_set);
625 } else if (*nkeyset == 0) {
626 krb5_warnx(context,
627 "failed to parse any of the [kadmin]default_keys values");
628 ret = EINVAL; /* XXX */
631 return ret;
635 krb5_error_code
636 hdb_generate_key_set_password(krb5_context context,
637 krb5_principal principal,
638 const char *password,
639 krb5_key_salt_tuple *ks_tuple, int n_ks_tuple,
640 Key **keys, size_t *num_keys)
642 krb5_error_code ret;
643 size_t i;
645 ret = hdb_generate_key_set(context, principal, ks_tuple, n_ks_tuple,
646 keys, num_keys, 0);
647 if (ret)
648 return ret;
650 for (i = 0; i < (*num_keys); i++) {
651 krb5_salt salt;
653 salt.salttype = (*keys)[i].salt->type;
654 salt.saltvalue.length = (*keys)[i].salt->salt.length;
655 salt.saltvalue.data = (*keys)[i].salt->salt.data;
657 ret = krb5_string_to_key_salt (context,
658 (*keys)[i].key.keytype,
659 password,
660 salt,
661 &(*keys)[i].key);
663 if(ret)
664 break;
667 if(ret) {
668 hdb_free_keys (context, *num_keys, *keys);
669 return ret;
671 return ret;