etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / hdb / keys.c
blobe5c243b1834d855d61668cec8242c9da206ba662
1 /* $NetBSD: keys.c,v 1.3 2014/11/26 10:12:27 pettai Exp $ */
4 /*
5 * Copyright (c) 1997 - 2001, 2003 - 2004 Kungliga Tekniska Högskolan
6 * (Royal Institute of Technology, Stockholm, Sweden).
7 * All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the Institute nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
37 #include "hdb_locl.h"
40 * free all the memory used by (len, keys)
43 void
44 hdb_free_keys (krb5_context context, int len, Key *keys)
46 int i;
48 for (i = 0; i < len; i++) {
49 free(keys[i].mkvno);
50 keys[i].mkvno = NULL;
51 if (keys[i].salt != NULL) {
52 free_Salt(keys[i].salt);
53 free(keys[i].salt);
54 keys[i].salt = NULL;
56 krb5_free_keyblock_contents(context, &keys[i].key);
58 free (keys);
62 * for each entry in `default_keys' try to parse it as a sequence
63 * of etype:salttype:salt, syntax of this if something like:
64 * [(des|des3|etype):](pw-salt|afs3)[:string], if etype is omitted it
65 * means all etypes, and if string is omitted is means the default
66 * string (for that principal). Additional special values:
67 * v5 == pw-salt, and
68 * v4 == des:pw-salt:
69 * afs or afs3 == des:afs3-salt
72 static const krb5_enctype des_etypes[] = {
73 ETYPE_DES_CBC_MD5,
74 ETYPE_DES_CBC_MD4,
75 ETYPE_DES_CBC_CRC
78 static const krb5_enctype all_etypes[] = {
79 ETYPE_AES256_CTS_HMAC_SHA1_96,
80 ETYPE_DES3_CBC_SHA1,
81 ETYPE_ARCFOUR_HMAC_MD5
84 static krb5_error_code
85 parse_key_set(krb5_context context, const char *key,
86 krb5_enctype **ret_enctypes, size_t *ret_num_enctypes,
87 krb5_salt *salt, krb5_principal principal)
89 const char *p;
90 char buf[3][256];
91 int num_buf = 0;
92 int i, num_enctypes = 0;
93 krb5_enctype e;
94 const krb5_enctype *enctypes = NULL;
95 krb5_error_code ret;
97 p = key;
99 *ret_enctypes = NULL;
100 *ret_num_enctypes = 0;
102 /* split p in a list of :-separated strings */
103 for(num_buf = 0; num_buf < 3; num_buf++)
104 if(strsep_copy(&p, ":", buf[num_buf], sizeof(buf[num_buf])) == -1)
105 break;
107 salt->saltvalue.data = NULL;
108 salt->saltvalue.length = 0;
110 for(i = 0; i < num_buf; i++) {
111 if(enctypes == NULL && num_buf > 1) {
112 /* this might be a etype specifier */
113 /* XXX there should be a string_to_etypes handling
114 special cases like `des' and `all' */
115 if(strcmp(buf[i], "des") == 0) {
116 enctypes = des_etypes;
117 num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]);
118 } else if(strcmp(buf[i], "des3") == 0) {
119 e = ETYPE_DES3_CBC_SHA1;
120 enctypes = &e;
121 num_enctypes = 1;
122 } else {
123 ret = krb5_string_to_enctype(context, buf[i], &e);
124 if (ret == 0) {
125 enctypes = &e;
126 num_enctypes = 1;
127 } else
128 return ret;
130 continue;
132 if(salt->salttype == 0) {
133 /* interpret string as a salt specifier, if no etype
134 is set, this sets default values */
135 /* XXX should perhaps use string_to_salttype, but that
136 interface sucks */
137 if(strcmp(buf[i], "pw-salt") == 0) {
138 if(enctypes == NULL) {
139 enctypes = all_etypes;
140 num_enctypes = sizeof(all_etypes)/sizeof(all_etypes[0]);
142 salt->salttype = KRB5_PW_SALT;
143 } else if(strcmp(buf[i], "afs3-salt") == 0) {
144 if(enctypes == NULL) {
145 enctypes = des_etypes;
146 num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]);
148 salt->salttype = KRB5_AFS3_SALT;
150 continue;
154 /* if there is a final string, use it as the string to
155 salt with, this is mostly useful with null salt for
156 v4 compat, and a cell name for afs compat */
157 salt->saltvalue.data = strdup(buf[i]);
158 if (salt->saltvalue.data == NULL) {
159 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
160 return ENOMEM;
162 salt->saltvalue.length = strlen(buf[i]);
166 if(enctypes == NULL || salt->salttype == 0) {
167 krb5_set_error_message(context, EINVAL, "bad value for default_keys `%s'", key);
168 return EINVAL;
171 /* if no salt was specified make up default salt */
172 if(salt->saltvalue.data == NULL) {
173 if(salt->salttype == KRB5_PW_SALT)
174 ret = krb5_get_pw_salt(context, principal, salt);
175 else if(salt->salttype == KRB5_AFS3_SALT) {
176 krb5_const_realm realm = krb5_principal_get_realm(context, principal);
177 salt->saltvalue.data = strdup(realm);
178 if(salt->saltvalue.data == NULL) {
179 krb5_set_error_message(context, ENOMEM,
180 "out of memory while "
181 "parsing salt specifiers");
182 return ENOMEM;
184 strlwr(salt->saltvalue.data);
185 salt->saltvalue.length = strlen(realm);
189 *ret_enctypes = malloc(sizeof(enctypes[0]) * num_enctypes);
190 if (*ret_enctypes == NULL) {
191 krb5_free_salt(context, *salt);
192 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
193 return ENOMEM;
195 memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * num_enctypes);
196 *ret_num_enctypes = num_enctypes;
198 return 0;
201 static krb5_error_code
202 add_enctype_to_key_set(Key **key_set, size_t *nkeyset,
203 krb5_enctype enctype, krb5_salt *salt)
205 krb5_error_code ret;
206 Key key, *tmp;
208 memset(&key, 0, sizeof(key));
210 tmp = realloc(*key_set, (*nkeyset + 1) * sizeof((*key_set)[0]));
211 if (tmp == NULL)
212 return ENOMEM;
214 *key_set = tmp;
216 key.key.keytype = enctype;
217 key.key.keyvalue.length = 0;
218 key.key.keyvalue.data = NULL;
220 if (salt) {
221 key.salt = calloc(1, sizeof(*key.salt));
222 if (key.salt == NULL) {
223 free_Key(&key);
224 return ENOMEM;
227 key.salt->type = salt->salttype;
228 krb5_data_zero (&key.salt->salt);
230 ret = krb5_data_copy(&key.salt->salt,
231 salt->saltvalue.data,
232 salt->saltvalue.length);
233 if (ret) {
234 free_Key(&key);
235 return ret;
237 } else
238 key.salt = NULL;
240 (*key_set)[*nkeyset] = key;
242 *nkeyset += 1;
244 return 0;
249 * Generate the `key_set' from the [kadmin]default_keys statement. If
250 * `no_salt' is set, salt is not important (and will not be set) since
251 * it's random keys that is going to be created.
254 krb5_error_code
255 hdb_generate_key_set(krb5_context context, krb5_principal principal,
256 Key **ret_key_set, size_t *nkeyset, int no_salt)
258 char **ktypes, **kp;
259 krb5_error_code ret;
260 Key *k, *key_set;
261 size_t i, j;
262 static const char *default_keytypes[] = {
263 "aes256-cts-hmac-sha1-96:pw-salt",
264 "des3-cbc-sha1:pw-salt",
265 "arcfour-hmac-md5:pw-salt",
266 NULL
269 ktypes = krb5_config_get_strings(context, NULL, "kadmin",
270 "default_keys", NULL);
271 if (ktypes == NULL)
272 ktypes = (char **)(intptr_t)default_keytypes;
274 *ret_key_set = key_set = NULL;
275 *nkeyset = 0;
277 ret = 0;
279 for(kp = ktypes; kp && *kp; kp++) {
280 const char *p;
281 krb5_salt salt;
282 krb5_enctype *enctypes;
283 size_t num_enctypes;
285 p = *kp;
286 /* check alias */
287 if(strcmp(p, "v5") == 0)
288 p = "pw-salt";
289 else if(strcmp(p, "v4") == 0)
290 p = "des:pw-salt:";
291 else if(strcmp(p, "afs") == 0 || strcmp(p, "afs3") == 0)
292 p = "des:afs3-salt";
293 else if (strcmp(p, "arcfour-hmac-md5") == 0)
294 p = "arcfour-hmac-md5:pw-salt";
296 memset(&salt, 0, sizeof(salt));
298 ret = parse_key_set(context, p,
299 &enctypes, &num_enctypes, &salt, principal);
300 if (ret) {
301 krb5_warn(context, ret, "bad value for default_keys `%s'", *kp);
302 ret = 0;
303 continue;
306 for (i = 0; i < num_enctypes; i++) {
307 /* find duplicates */
308 for (j = 0; j < *nkeyset; j++) {
310 k = &key_set[j];
312 if (k->key.keytype == enctypes[i]) {
313 if (no_salt)
314 break;
315 if (k->salt == NULL && salt.salttype == KRB5_PW_SALT)
316 break;
317 if (k->salt->type == salt.salttype &&
318 k->salt->salt.length == salt.saltvalue.length &&
319 memcmp(k->salt->salt.data, salt.saltvalue.data,
320 salt.saltvalue.length) == 0)
321 break;
324 /* not a duplicate, lets add it */
325 if (j == *nkeyset) {
326 ret = add_enctype_to_key_set(&key_set, nkeyset, enctypes[i],
327 no_salt ? NULL : &salt);
328 if (ret) {
329 free(enctypes);
330 krb5_free_salt(context, salt);
331 goto out;
335 free(enctypes);
336 krb5_free_salt(context, salt);
339 *ret_key_set = key_set;
341 out:
342 if (ktypes != (char **)(intptr_t)default_keytypes)
343 krb5_config_free_strings(ktypes);
345 if (ret) {
346 krb5_warn(context, ret,
347 "failed to parse the [kadmin]default_keys values");
349 for (i = 0; i < *nkeyset; i++)
350 free_Key(&key_set[i]);
351 free(key_set);
352 } else if (*nkeyset == 0) {
353 krb5_warnx(context,
354 "failed to parse any of the [kadmin]default_keys values");
355 ret = EINVAL; /* XXX */
358 return ret;
362 krb5_error_code
363 hdb_generate_key_set_password(krb5_context context,
364 krb5_principal principal,
365 const char *password,
366 Key **keys, size_t *num_keys)
368 krb5_error_code ret;
369 size_t i;
371 ret = hdb_generate_key_set(context, principal,
372 keys, num_keys, 0);
373 if (ret)
374 return ret;
376 for (i = 0; i < (*num_keys); i++) {
377 krb5_salt salt;
379 salt.salttype = (*keys)[i].salt->type;
380 salt.saltvalue.length = (*keys)[i].salt->salt.length;
381 salt.saltvalue.data = (*keys)[i].salt->salt.data;
383 ret = krb5_string_to_key_salt (context,
384 (*keys)[i].key.keytype,
385 password,
386 salt,
387 &(*keys)[i].key);
389 if(ret)
390 break;
393 if(ret) {
394 hdb_free_keys (context, *num_keys, *keys);
395 return ret;
397 return ret;