xref: /minix3/crypto/external/bsd/heimdal/dist/lib/kadm5/set_keys.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: set_keys.c,v 1.1.1.2 2014/04/24 12:45:49 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997 - 2001, 2003 Kungliga Tekniska Högskolan
5ebfedea0SLionel Sambuc  * (Royal Institute of Technology, Stockholm, Sweden).
6ebfedea0SLionel Sambuc  * All rights reserved.
7ebfedea0SLionel Sambuc  *
8ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
9ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
10ebfedea0SLionel Sambuc  * are met:
11ebfedea0SLionel Sambuc  *
12ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
13ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
14ebfedea0SLionel Sambuc  *
15ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
16ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
17ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
18ebfedea0SLionel Sambuc  *
19ebfedea0SLionel Sambuc  * 3. Neither the name of the Institute nor the names of its contributors
20ebfedea0SLionel Sambuc  *    may be used to endorse or promote products derived from this software
21ebfedea0SLionel Sambuc  *    without specific prior written permission.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ebfedea0SLionel Sambuc  * SUCH DAMAGE.
34ebfedea0SLionel Sambuc  */
35ebfedea0SLionel Sambuc 
36ebfedea0SLionel Sambuc #include "kadm5_locl.h"
37ebfedea0SLionel Sambuc 
38*0a6a1f1dSLionel Sambuc __RCSID("NetBSD");
39ebfedea0SLionel Sambuc 
40ebfedea0SLionel Sambuc /*
41ebfedea0SLionel Sambuc  * Set the keys of `ent' to the string-to-key of `password'
42ebfedea0SLionel Sambuc  */
43ebfedea0SLionel Sambuc 
44ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_set_keys(kadm5_server_context * context,hdb_entry * ent,const char * password)45ebfedea0SLionel Sambuc _kadm5_set_keys(kadm5_server_context *context,
46ebfedea0SLionel Sambuc 		hdb_entry *ent,
47ebfedea0SLionel Sambuc 		const char *password)
48ebfedea0SLionel Sambuc {
49ebfedea0SLionel Sambuc     Key *keys;
50ebfedea0SLionel Sambuc     size_t num_keys;
51ebfedea0SLionel Sambuc     kadm5_ret_t ret;
52ebfedea0SLionel Sambuc 
53ebfedea0SLionel Sambuc     ret = hdb_generate_key_set_password(context->context,
54ebfedea0SLionel Sambuc 					ent->principal,
55ebfedea0SLionel Sambuc 					password, &keys, &num_keys);
56ebfedea0SLionel Sambuc     if (ret)
57ebfedea0SLionel Sambuc 	return ret;
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc     _kadm5_free_keys (context->context, ent->keys.len, ent->keys.val);
60ebfedea0SLionel Sambuc     ent->keys.val = keys;
61ebfedea0SLionel Sambuc     ent->keys.len = num_keys;
62ebfedea0SLionel Sambuc 
63ebfedea0SLionel Sambuc     hdb_entry_set_pw_change_time(context->context, ent, 0);
64ebfedea0SLionel Sambuc 
65ebfedea0SLionel Sambuc     if (krb5_config_get_bool_default(context->context, NULL, FALSE,
66ebfedea0SLionel Sambuc 				     "kadmin", "save-password", NULL))
67ebfedea0SLionel Sambuc     {
68ebfedea0SLionel Sambuc 	ret = hdb_entry_set_password(context->context, context->db,
69ebfedea0SLionel Sambuc 				     ent, password);
70ebfedea0SLionel Sambuc 	if (ret)
71ebfedea0SLionel Sambuc 	    return ret;
72ebfedea0SLionel Sambuc     }
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc     return 0;
75ebfedea0SLionel Sambuc }
76ebfedea0SLionel Sambuc 
77ebfedea0SLionel Sambuc /*
78ebfedea0SLionel Sambuc  * Set the keys of `ent' to (`n_key_data', `key_data')
79ebfedea0SLionel Sambuc  */
80ebfedea0SLionel Sambuc 
81ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_set_keys2(kadm5_server_context * context,hdb_entry * ent,int16_t n_key_data,krb5_key_data * key_data)82ebfedea0SLionel Sambuc _kadm5_set_keys2(kadm5_server_context *context,
83ebfedea0SLionel Sambuc 		 hdb_entry *ent,
84ebfedea0SLionel Sambuc 		 int16_t n_key_data,
85ebfedea0SLionel Sambuc 		 krb5_key_data *key_data)
86ebfedea0SLionel Sambuc {
87ebfedea0SLionel Sambuc     krb5_error_code ret;
88ebfedea0SLionel Sambuc     int i;
89ebfedea0SLionel Sambuc     unsigned len;
90ebfedea0SLionel Sambuc     Key *keys;
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc     len  = n_key_data;
93ebfedea0SLionel Sambuc     keys = malloc (len * sizeof(*keys));
94ebfedea0SLionel Sambuc     if (keys == NULL && len != 0)
95ebfedea0SLionel Sambuc 	return ENOMEM;
96ebfedea0SLionel Sambuc 
97ebfedea0SLionel Sambuc     _kadm5_init_keys (keys, len);
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc     for(i = 0; i < n_key_data; i++) {
100ebfedea0SLionel Sambuc 	keys[i].mkvno = NULL;
101ebfedea0SLionel Sambuc 	keys[i].key.keytype = key_data[i].key_data_type[0];
102ebfedea0SLionel Sambuc 	ret = krb5_data_copy(&keys[i].key.keyvalue,
103ebfedea0SLionel Sambuc 			     key_data[i].key_data_contents[0],
104ebfedea0SLionel Sambuc 			     key_data[i].key_data_length[0]);
105ebfedea0SLionel Sambuc 	if(ret)
106ebfedea0SLionel Sambuc 	    goto out;
107ebfedea0SLionel Sambuc 	if(key_data[i].key_data_ver == 2) {
108ebfedea0SLionel Sambuc 	    Salt *salt;
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc 	    salt = calloc(1, sizeof(*salt));
111ebfedea0SLionel Sambuc 	    if(salt == NULL) {
112ebfedea0SLionel Sambuc 		ret = ENOMEM;
113ebfedea0SLionel Sambuc 		goto out;
114ebfedea0SLionel Sambuc 	    }
115ebfedea0SLionel Sambuc 	    keys[i].salt = salt;
116ebfedea0SLionel Sambuc 	    salt->type = key_data[i].key_data_type[1];
117ebfedea0SLionel Sambuc 	    krb5_data_copy(&salt->salt,
118ebfedea0SLionel Sambuc 			   key_data[i].key_data_contents[1],
119ebfedea0SLionel Sambuc 			   key_data[i].key_data_length[1]);
120ebfedea0SLionel Sambuc 	} else
121ebfedea0SLionel Sambuc 	    keys[i].salt = NULL;
122ebfedea0SLionel Sambuc     }
123ebfedea0SLionel Sambuc     _kadm5_free_keys (context->context, ent->keys.len, ent->keys.val);
124ebfedea0SLionel Sambuc     ent->keys.len = len;
125ebfedea0SLionel Sambuc     ent->keys.val = keys;
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc     hdb_entry_set_pw_change_time(context->context, ent, 0);
128ebfedea0SLionel Sambuc     hdb_entry_clear_password(context->context, ent);
129ebfedea0SLionel Sambuc 
130ebfedea0SLionel Sambuc     return 0;
131ebfedea0SLionel Sambuc  out:
132ebfedea0SLionel Sambuc     _kadm5_free_keys (context->context, len, keys);
133ebfedea0SLionel Sambuc     return ret;
134ebfedea0SLionel Sambuc }
135ebfedea0SLionel Sambuc 
136ebfedea0SLionel Sambuc /*
137ebfedea0SLionel Sambuc  * Set the keys of `ent' to `n_keys, keys'
138ebfedea0SLionel Sambuc  */
139ebfedea0SLionel Sambuc 
140ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_set_keys3(kadm5_server_context * context,hdb_entry * ent,int n_keys,krb5_keyblock * keyblocks)141ebfedea0SLionel Sambuc _kadm5_set_keys3(kadm5_server_context *context,
142ebfedea0SLionel Sambuc 		 hdb_entry *ent,
143ebfedea0SLionel Sambuc 		 int n_keys,
144ebfedea0SLionel Sambuc 		 krb5_keyblock *keyblocks)
145ebfedea0SLionel Sambuc {
146ebfedea0SLionel Sambuc     krb5_error_code ret;
147ebfedea0SLionel Sambuc     int i;
148ebfedea0SLionel Sambuc     unsigned len;
149ebfedea0SLionel Sambuc     Key *keys;
150ebfedea0SLionel Sambuc 
151ebfedea0SLionel Sambuc     len  = n_keys;
152ebfedea0SLionel Sambuc     keys = malloc (len * sizeof(*keys));
153ebfedea0SLionel Sambuc     if (keys == NULL && len != 0)
154ebfedea0SLionel Sambuc 	return ENOMEM;
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc     _kadm5_init_keys (keys, len);
157ebfedea0SLionel Sambuc 
158ebfedea0SLionel Sambuc     for(i = 0; i < n_keys; i++) {
159ebfedea0SLionel Sambuc 	keys[i].mkvno = NULL;
160ebfedea0SLionel Sambuc 	ret = krb5_copy_keyblock_contents (context->context,
161ebfedea0SLionel Sambuc 					   &keyblocks[i],
162ebfedea0SLionel Sambuc 					   &keys[i].key);
163ebfedea0SLionel Sambuc 	if(ret)
164ebfedea0SLionel Sambuc 	    goto out;
165ebfedea0SLionel Sambuc 	keys[i].salt = NULL;
166ebfedea0SLionel Sambuc     }
167ebfedea0SLionel Sambuc     _kadm5_free_keys (context->context, ent->keys.len, ent->keys.val);
168ebfedea0SLionel Sambuc     ent->keys.len = len;
169ebfedea0SLionel Sambuc     ent->keys.val = keys;
170ebfedea0SLionel Sambuc 
171ebfedea0SLionel Sambuc     hdb_entry_set_pw_change_time(context->context, ent, 0);
172ebfedea0SLionel Sambuc     hdb_entry_clear_password(context->context, ent);
173ebfedea0SLionel Sambuc 
174ebfedea0SLionel Sambuc     return 0;
175ebfedea0SLionel Sambuc  out:
176ebfedea0SLionel Sambuc     _kadm5_free_keys (context->context, len, keys);
177ebfedea0SLionel Sambuc     return ret;
178ebfedea0SLionel Sambuc }
179ebfedea0SLionel Sambuc 
180ebfedea0SLionel Sambuc /*
181ebfedea0SLionel Sambuc  *
182ebfedea0SLionel Sambuc  */
183ebfedea0SLionel Sambuc 
184ebfedea0SLionel Sambuc static int
is_des_key_p(int keytype)185ebfedea0SLionel Sambuc is_des_key_p(int keytype)
186ebfedea0SLionel Sambuc {
187ebfedea0SLionel Sambuc     return keytype == ETYPE_DES_CBC_CRC ||
188ebfedea0SLionel Sambuc     	keytype == ETYPE_DES_CBC_MD4 ||
189ebfedea0SLionel Sambuc 	keytype == ETYPE_DES_CBC_MD5;
190ebfedea0SLionel Sambuc }
191ebfedea0SLionel Sambuc 
192ebfedea0SLionel Sambuc 
193ebfedea0SLionel Sambuc /*
194ebfedea0SLionel Sambuc  * Set the keys of `ent' to random keys and return them in `n_keys'
195ebfedea0SLionel Sambuc  * and `new_keys'.
196ebfedea0SLionel Sambuc  */
197ebfedea0SLionel Sambuc 
198ebfedea0SLionel Sambuc kadm5_ret_t
_kadm5_set_keys_randomly(kadm5_server_context * context,hdb_entry * ent,krb5_keyblock ** new_keys,int * n_keys)199ebfedea0SLionel Sambuc _kadm5_set_keys_randomly (kadm5_server_context *context,
200ebfedea0SLionel Sambuc 			  hdb_entry *ent,
201ebfedea0SLionel Sambuc 			  krb5_keyblock **new_keys,
202ebfedea0SLionel Sambuc 			  int *n_keys)
203ebfedea0SLionel Sambuc {
204ebfedea0SLionel Sambuc    krb5_keyblock *kblock = NULL;
205ebfedea0SLionel Sambuc    kadm5_ret_t ret = 0;
206*0a6a1f1dSLionel Sambuc    int des_keyblock;
207*0a6a1f1dSLionel Sambuc    size_t i, num_keys;
208ebfedea0SLionel Sambuc    Key *keys;
209ebfedea0SLionel Sambuc 
210ebfedea0SLionel Sambuc    ret = hdb_generate_key_set(context->context, ent->principal,
211ebfedea0SLionel Sambuc 			       &keys, &num_keys, 1);
212ebfedea0SLionel Sambuc    if (ret)
213ebfedea0SLionel Sambuc 	return ret;
214ebfedea0SLionel Sambuc 
215ebfedea0SLionel Sambuc    kblock = malloc(num_keys * sizeof(kblock[0]));
216ebfedea0SLionel Sambuc    if (kblock == NULL) {
217ebfedea0SLionel Sambuc 	ret = ENOMEM;
218ebfedea0SLionel Sambuc 	_kadm5_free_keys (context->context, num_keys, keys);
219ebfedea0SLionel Sambuc 	return ret;
220ebfedea0SLionel Sambuc    }
221ebfedea0SLionel Sambuc    memset(kblock, 0, num_keys * sizeof(kblock[0]));
222ebfedea0SLionel Sambuc 
223ebfedea0SLionel Sambuc    des_keyblock = -1;
224ebfedea0SLionel Sambuc    for (i = 0; i < num_keys; i++) {
225ebfedea0SLionel Sambuc 
226ebfedea0SLionel Sambuc 	/*
227ebfedea0SLionel Sambuc 	 * To make sure all des keys are the the same we generate only
228ebfedea0SLionel Sambuc 	 * the first one and then copy key to all other des keys.
229ebfedea0SLionel Sambuc 	 */
230ebfedea0SLionel Sambuc 
231ebfedea0SLionel Sambuc 	if (des_keyblock != -1 && is_des_key_p(keys[i].key.keytype)) {
232ebfedea0SLionel Sambuc 	    ret = krb5_copy_keyblock_contents (context->context,
233ebfedea0SLionel Sambuc 					       &kblock[des_keyblock],
234ebfedea0SLionel Sambuc 					       &kblock[i]);
235ebfedea0SLionel Sambuc 	    if (ret)
236ebfedea0SLionel Sambuc 		goto out;
237ebfedea0SLionel Sambuc 	    kblock[i].keytype = keys[i].key.keytype;
238ebfedea0SLionel Sambuc 	} else {
239ebfedea0SLionel Sambuc 	    ret = krb5_generate_random_keyblock (context->context,
240ebfedea0SLionel Sambuc 						 keys[i].key.keytype,
241ebfedea0SLionel Sambuc 						 &kblock[i]);
242ebfedea0SLionel Sambuc 	    if (ret)
243ebfedea0SLionel Sambuc 		goto out;
244ebfedea0SLionel Sambuc 
245ebfedea0SLionel Sambuc 	    if (is_des_key_p(keys[i].key.keytype))
246ebfedea0SLionel Sambuc 		des_keyblock = i;
247ebfedea0SLionel Sambuc 	}
248ebfedea0SLionel Sambuc 
249ebfedea0SLionel Sambuc 	ret = krb5_copy_keyblock_contents (context->context,
250ebfedea0SLionel Sambuc 					   &kblock[i],
251ebfedea0SLionel Sambuc 					   &keys[i].key);
252ebfedea0SLionel Sambuc 	if (ret)
253ebfedea0SLionel Sambuc 	    goto out;
254ebfedea0SLionel Sambuc    }
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc out:
257ebfedea0SLionel Sambuc    if(ret) {
258ebfedea0SLionel Sambuc 	for (i = 0; i < num_keys; ++i)
259ebfedea0SLionel Sambuc 	    krb5_free_keyblock_contents (context->context, &kblock[i]);
260ebfedea0SLionel Sambuc 	free(kblock);
261ebfedea0SLionel Sambuc 	_kadm5_free_keys (context->context, num_keys, keys);
262ebfedea0SLionel Sambuc 	return ret;
263ebfedea0SLionel Sambuc    }
264ebfedea0SLionel Sambuc 
265ebfedea0SLionel Sambuc    _kadm5_free_keys (context->context, ent->keys.len, ent->keys.val);
266ebfedea0SLionel Sambuc    ent->keys.val = keys;
267ebfedea0SLionel Sambuc    ent->keys.len = num_keys;
268ebfedea0SLionel Sambuc    *new_keys     = kblock;
269ebfedea0SLionel Sambuc    *n_keys       = num_keys;
270ebfedea0SLionel Sambuc 
271ebfedea0SLionel Sambuc    hdb_entry_set_pw_change_time(context->context, ent, 0);
272ebfedea0SLionel Sambuc    hdb_entry_clear_password(context->context, ent);
273ebfedea0SLionel Sambuc 
274ebfedea0SLionel Sambuc    return 0;
275ebfedea0SLionel Sambuc }
276