2 * Copyright (c) 1997, 1998, 1999 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
34 #include "kadm5_locl.h"
36 RCSID("$Id: set_keys.c,v 1.18 1999/12/04 23:11:01 assar Exp $");
39 * free all the memory used by (len, keys)
43 free_keys (kadm5_server_context *context,
48 for (i = 0; i < len; ++i) {
51 if (keys[i].salt != NULL) {
52 free_Salt(keys[i].salt);
56 krb5_free_keyblock_contents(context->context, &keys[i].key);
62 * null-ify `len', `keys'
66 init_keys (Key *keys, int len)
70 for (i = 0; i < len; ++i) {
73 keys[i].key.keyvalue.length = 0;
74 keys[i].key.keyvalue.data = NULL;
79 * the known and used DES enctypes
82 static krb5_enctype des_types[] = { ETYPE_DES_CBC_CRC,
86 static unsigned n_des_types = 3;
89 * Set the keys of `ent' to the string-to-key of `password'
93 _kadm5_set_keys(kadm5_server_context *context,
102 krb5_boolean v4_salt = FALSE;
104 len = n_des_types + 1;
105 keys = malloc (len * sizeof(*keys));
109 init_keys (keys, len);
111 salt.salttype = KRB5_PW_SALT;
112 salt.saltvalue.length = 0;
113 salt.saltvalue.data = NULL;
115 if (krb5_config_get_bool (context->context,
116 NULL, "kadmin", "use_v4_salt", NULL)) {
119 ret = krb5_get_pw_salt (context->context, ent->principal, &salt);
124 for (i = 0; i < n_des_types; ++i) {
125 ret = krb5_string_to_key_salt (context->context,
133 keys[i].salt = malloc (sizeof(*keys[i].salt));
134 if (keys[i].salt == NULL) {
138 keys[i].salt->type = salt.salttype;
139 ret = copy_octet_string (&salt.saltvalue, &keys[i].salt->salt);
145 ret = krb5_string_to_key (context->context,
149 &keys[n_des_types].key);
153 free_keys (context, ent->keys.len, ent->keys.val);
155 ent->keys.val = keys;
159 krb5_data_free (&salt.saltvalue);
160 free_keys (context, len, keys);
165 * Set the keys of `ent' to (`n_key_data', `key_data')
169 _kadm5_set_keys2(hdb_entry *ent,
171 krb5_key_data *key_data)
176 ent->keys.len = n_key_data;
177 ent->keys.val = malloc(ent->keys.len * sizeof(*ent->keys.val));
178 if(ent->keys.val == NULL)
180 for(i = 0; i < n_key_data; i++) {
181 ent->keys.val[i].mkvno = NULL;
182 ent->keys.val[i].key.keytype = key_data[i].key_data_type[0];
183 ret = krb5_data_copy(&ent->keys.val[i].key.keyvalue,
184 key_data[i].key_data_contents[0],
185 key_data[i].key_data_length[0]);
188 if(key_data[i].key_data_ver == 2) {
190 salt = malloc(sizeof(*salt));
193 ent->keys.val[i].salt = salt;
194 salt->type = key_data[i].key_data_type[1];
195 krb5_data_copy(&salt->salt,
196 key_data[i].key_data_contents[1],
197 key_data[i].key_data_length[1]);
199 ent->keys.val[i].salt = NULL;
206 * Set the keys of `ent' to random keys and return them in `n_keys'
211 _kadm5_set_keys_randomly (kadm5_server_context *context,
213 krb5_keyblock **new_keys,
222 len = n_des_types + 1;
223 keys = malloc (len * sizeof(*keys));
227 for (i = 0; i < len; ++i) {
228 keys[i].keyvalue.length = 0;
229 keys[i].keyvalue.data = NULL;
232 hkeys = malloc (len * sizeof(*hkeys));
238 init_keys (hkeys, len);
240 ret = krb5_generate_random_keyblock (context->context,
246 ret = krb5_copy_keyblock_contents (context->context,
252 for (i = 1; i < n_des_types; ++i) {
253 ret = krb5_copy_keyblock_contents (context->context,
258 keys[i].keytype = des_types[i];
259 ret = krb5_copy_keyblock_contents (context->context,
264 hkeys[i].key.keytype = des_types[i];
267 ret = krb5_generate_random_keyblock (context->context,
273 ret = krb5_copy_keyblock_contents (context->context,
275 &hkeys[n_des_types].key);
279 free_keys (context, ent->keys.len, ent->keys.val);
281 ent->keys.val = hkeys;
287 for (i = 0; i < len; ++i)
288 krb5_free_keyblock_contents (context->context, &keys[i]);
290 free_keys (context, len, hkeys);