xref: /freebsd/crypto/heimdal/lib/hdb/keys.c (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 
2 /*
3  * Copyright (c) 1997 - 2001, 2003 - 2004 Kungliga Tekniska Högskolan
4  * (Royal Institute of Technology, Stockholm, Sweden).
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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.
17  *
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.
21  *
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.
33  */
34 
35 #include "hdb_locl.h"
36 
37 /*
38  * free all the memory used by (len, keys)
39  */
40 
41 void
42 hdb_free_keys (krb5_context context, int len, Key *keys)
43 {
44     int i;
45 
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;
53 	}
54 	krb5_free_keyblock_contents(context, &keys[i].key);
55     }
56     free (keys);
57 }
58 
59 /*
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
68  */
69 
70 static const krb5_enctype des_etypes[] = {
71     ETYPE_DES_CBC_MD5,
72     ETYPE_DES_CBC_MD4,
73     ETYPE_DES_CBC_CRC
74 };
75 
76 static const krb5_enctype all_etypes[] = {
77     ETYPE_AES256_CTS_HMAC_SHA1_96,
78     ETYPE_ARCFOUR_HMAC_MD5,
79     ETYPE_DES3_CBC_SHA1
80 };
81 
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)
86 {
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;
94 
95     p = key;
96 
97     *ret_enctypes = NULL;
98     *ret_num_enctypes = 0;
99 
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;
104 
105     salt->saltvalue.data = NULL;
106     salt->saltvalue.length = 0;
107 
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 = ETYPE_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;
127 	    }
128 	    continue;
129 	}
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]);
139 		}
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]);
145 		}
146 		salt->salttype = KRB5_AFS3_SALT;
147 	    }
148 	    continue;
149 	}
150 
151 	{
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;
159 	    }
160 	    salt->saltvalue.length = strlen(buf[i]);
161 	}
162     }
163 
164     if(enctypes == NULL || salt->salttype == 0) {
165 	krb5_set_error_message(context, EINVAL, "bad value for default_keys `%s'", key);
166 	return EINVAL;
167     }
168 
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;
181 	    }
182 	    strlwr(salt->saltvalue.data);
183 	    salt->saltvalue.length = strlen(realm);
184 	}
185     }
186 
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;
192     }
193     memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * num_enctypes);
194     *ret_num_enctypes = num_enctypes;
195 
196     return 0;
197 }
198 
199 static krb5_error_code
200 add_enctype_to_key_set(Key **key_set, size_t *nkeyset,
201 		       krb5_enctype enctype, krb5_salt *salt)
202 {
203     krb5_error_code ret;
204     Key key, *tmp;
205 
206     memset(&key, 0, sizeof(key));
207 
208     tmp = realloc(*key_set, (*nkeyset + 1) * sizeof((*key_set)[0]));
209     if (tmp == NULL)
210 	return ENOMEM;
211 
212     *key_set = tmp;
213 
214     key.key.keytype = enctype;
215     key.key.keyvalue.length = 0;
216     key.key.keyvalue.data = NULL;
217 
218     if (salt) {
219 	key.salt = calloc(1, sizeof(*key.salt));
220 	if (key.salt == NULL) {
221 	    free_Key(&key);
222 	    return ENOMEM;
223 	}
224 
225 	key.salt->type = salt->salttype;
226 	krb5_data_zero (&key.salt->salt);
227 
228 	ret = krb5_data_copy(&key.salt->salt,
229 			     salt->saltvalue.data,
230 			     salt->saltvalue.length);
231 	if (ret) {
232 	    free_Key(&key);
233 	    return ret;
234 	}
235     } else
236 	key.salt = NULL;
237 
238     (*key_set)[*nkeyset] = key;
239 
240     *nkeyset += 1;
241 
242     return 0;
243 }
244 
245 
246 /*
247  * Generate the `key_set' from the [kadmin]default_keys statement. If
248  * `no_salt' is set, salt is not important (and will not be set) since
249  * it's random keys that is going to be created.
250  */
251 
252 krb5_error_code
253 hdb_generate_key_set(krb5_context context, krb5_principal principal,
254 		     Key **ret_key_set, size_t *nkeyset, int no_salt)
255 {
256     char **ktypes, **kp;
257     krb5_error_code ret;
258     Key *k, *key_set;
259     size_t i, j;
260     static const char *default_keytypes[] = {
261 	"aes256-cts-hmac-sha1-96:pw-salt",
262 	"des3-cbc-sha1:pw-salt",
263 	"arcfour-hmac-md5:pw-salt",
264 	NULL
265     };
266 
267     ktypes = krb5_config_get_strings(context, NULL, "kadmin",
268 				     "default_keys", NULL);
269     if (ktypes == NULL)
270 	ktypes = (char **)(intptr_t)default_keytypes;
271 
272     *ret_key_set = key_set = NULL;
273     *nkeyset = 0;
274 
275     ret = 0;
276 
277     for(kp = ktypes; kp && *kp; kp++) {
278 	const char *p;
279 	krb5_salt salt;
280 	krb5_enctype *enctypes;
281 	size_t num_enctypes;
282 
283 	p = *kp;
284 	/* check alias */
285 	if(strcmp(p, "v5") == 0)
286 	    p = "pw-salt";
287 	else if(strcmp(p, "v4") == 0)
288 	    p = "des:pw-salt:";
289 	else if(strcmp(p, "afs") == 0 || strcmp(p, "afs3") == 0)
290 	    p = "des:afs3-salt";
291 	else if (strcmp(p, "arcfour-hmac-md5") == 0)
292 	    p = "arcfour-hmac-md5:pw-salt";
293 
294 	memset(&salt, 0, sizeof(salt));
295 
296 	ret = parse_key_set(context, p,
297 			    &enctypes, &num_enctypes, &salt, principal);
298 	if (ret) {
299 	    krb5_warn(context, ret, "bad value for default_keys `%s'", *kp);
300 	    ret = 0;
301 	    continue;
302 	}
303 
304 	for (i = 0; i < num_enctypes; i++) {
305 	    /* find duplicates */
306 	    for (j = 0; j < *nkeyset; j++) {
307 
308 		k = &key_set[j];
309 
310 		if (k->key.keytype == enctypes[i]) {
311 		    if (no_salt)
312 			break;
313 		    if (k->salt == NULL && salt.salttype == KRB5_PW_SALT)
314 			break;
315 		    if (k->salt->type == salt.salttype &&
316 			k->salt->salt.length == salt.saltvalue.length &&
317 			memcmp(k->salt->salt.data, salt.saltvalue.data,
318 			       salt.saltvalue.length) == 0)
319 			break;
320 		}
321 	    }
322 	    /* not a duplicate, lets add it */
323 	    if (j == *nkeyset) {
324 		ret = add_enctype_to_key_set(&key_set, nkeyset, enctypes[i],
325 					     no_salt ? NULL : &salt);
326 		if (ret) {
327 		    free(enctypes);
328 		    krb5_free_salt(context, salt);
329 		    goto out;
330 		}
331 	    }
332 	}
333 	free(enctypes);
334 	krb5_free_salt(context, salt);
335     }
336 
337     *ret_key_set = key_set;
338 
339  out:
340     if (ktypes != (char **)(intptr_t)default_keytypes)
341 	krb5_config_free_strings(ktypes);
342 
343     if (ret) {
344 	krb5_warn(context, ret,
345 		  "failed to parse the [kadmin]default_keys values");
346 
347 	for (i = 0; i < *nkeyset; i++)
348 	    free_Key(&key_set[i]);
349 	free(key_set);
350     } else if (*nkeyset == 0) {
351 	krb5_warnx(context,
352 		   "failed to parse any of the [kadmin]default_keys values");
353 	ret = EINVAL; /* XXX */
354     }
355 
356     return ret;
357 }
358 
359 
360 krb5_error_code
361 hdb_generate_key_set_password(krb5_context context,
362 			      krb5_principal principal,
363 			      const char *password,
364 			      Key **keys, size_t *num_keys)
365 {
366     krb5_error_code ret;
367     size_t i;
368 
369     ret = hdb_generate_key_set(context, principal,
370 				keys, num_keys, 0);
371     if (ret)
372 	return ret;
373 
374     for (i = 0; i < (*num_keys); i++) {
375 	krb5_salt salt;
376 
377 	salt.salttype = (*keys)[i].salt->type;
378 	salt.saltvalue.length = (*keys)[i].salt->salt.length;
379 	salt.saltvalue.data = (*keys)[i].salt->salt.data;
380 
381 	ret = krb5_string_to_key_salt (context,
382 				       (*keys)[i].key.keytype,
383 				       password,
384 				       salt,
385 				       &(*keys)[i].key);
386 
387 	if(ret)
388 	    break;
389     }
390 
391     if(ret) {
392 	hdb_free_keys (context, *num_keys, *keys);
393 	return ret;
394     }
395     return ret;
396 }
397