9b870501201dccd40de18d11c8d13d5172ca9e99
[jelmer/samba4-debian.git] / source / heimdal / lib / hdb / keys.c
1 /*
2  * Copyright (c) 1997 - 2001, 2003 - 2004 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden). 
4  * All rights reserved. 
5  *
6  * Redistribution and use in source and binary forms, with or without 
7  * modification, are permitted provided that the following conditions 
8  * are met: 
9  *
10  * 1. Redistributions of source code must retain the above copyright 
11  *    notice, this list of conditions and the following disclaimer. 
12  *
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. 
16  *
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. 
20  *
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 
31  * SUCH DAMAGE. 
32  */
33
34 #include "hdb_locl.h"
35
36 RCSID("$Id: keys.c 18819 2006-10-22 09:40:12Z lha $");
37
38 /*
39  * free all the memory used by (len, keys)
40  */
41
42 void
43 hdb_free_keys (krb5_context context, int len, Key *keys)
44 {
45     int i;
46
47     for (i = 0; i < len; i++) {
48         free(keys[i].mkvno);
49         keys[i].mkvno = NULL;
50         if (keys[i].salt != NULL) {
51             free_Salt(keys[i].salt);
52             free(keys[i].salt);
53             keys[i].salt = NULL;
54         }
55         krb5_free_keyblock_contents(context, &keys[i].key);
56     }
57     free (keys);
58 }
59
60 /* 
61  * for each entry in `default_keys' try to parse it as a sequence
62  * of etype:salttype:salt, syntax of this if something like:
63  * [(des|des3|etype):](pw-salt|afs3)[:string], if etype is omitted it
64  *      means all etypes, and if string is omitted is means the default
65  * string (for that principal). Additional special values:
66  *      v5 == pw-salt, and
67  *      v4 == des:pw-salt:
68  *      afs or afs3 == des:afs3-salt
69  */
70
71 /* the 3 DES types must be first */
72 static const krb5_enctype all_etypes[] = { 
73     ETYPE_DES_CBC_MD5,
74     ETYPE_DES_CBC_MD4,
75     ETYPE_DES_CBC_CRC,
76     ETYPE_AES256_CTS_HMAC_SHA1_96,
77     ETYPE_ARCFOUR_HMAC_MD5,
78     ETYPE_DES3_CBC_SHA1
79 };
80
81 static krb5_error_code
82 parse_key_set(krb5_context context, const char *key, 
83               krb5_enctype **ret_enctypes, size_t *ret_num_enctypes, 
84               krb5_salt *salt, krb5_principal principal)
85 {
86     const char *p;
87     char buf[3][256];
88     int num_buf = 0;
89     int i, num_enctypes = 0;
90     krb5_enctype e;
91     const krb5_enctype *enctypes = NULL;
92     krb5_error_code ret;
93     
94     p = key;
95
96     *ret_enctypes = NULL;
97     *ret_num_enctypes = 0;
98
99     /* split p in a list of :-separated strings */
100     for(num_buf = 0; num_buf < 3; num_buf++)
101         if(strsep_copy(&p, ":", buf[num_buf], sizeof(buf[num_buf])) == -1)
102             break;
103
104     salt->saltvalue.data = NULL;
105     salt->saltvalue.length = 0;
106
107     for(i = 0; i < num_buf; i++) {
108         if(enctypes == NULL) {
109             /* this might be a etype specifier */
110             /* XXX there should be a string_to_etypes handling
111                special cases like `des' and `all' */
112             if(strcmp(buf[i], "des") == 0) {
113                 enctypes = all_etypes;
114                 num_enctypes = 3;
115             } else if(strcmp(buf[i], "des3") == 0) {
116                 e = ETYPE_DES3_CBC_SHA1;
117                 enctypes = &e;
118                 num_enctypes = 1;
119             } else {
120                 ret = krb5_string_to_enctype(context, buf[i], &e);
121                 if (ret == 0) {
122                     enctypes = &e;
123                     num_enctypes = 1;
124                 } else
125                     return ret;
126             }
127         } else if(salt->salttype == 0) {
128             /* interpret string as a salt specifier, if no etype
129                is set, this sets default values */
130             /* XXX should perhaps use string_to_salttype, but that
131                interface sucks */
132             if(strcmp(buf[i], "pw-salt") == 0) {
133                 if(enctypes == NULL) {
134                     enctypes = all_etypes;
135                     num_enctypes = sizeof(all_etypes)/sizeof(all_etypes[0]);
136                 }
137                 salt->salttype = KRB5_PW_SALT;
138             } else if(strcmp(buf[i], "afs3-salt") == 0) {
139                 if(enctypes == NULL) {
140                     enctypes = all_etypes;
141                     num_enctypes = 3;
142                 }
143                 salt->salttype = KRB5_AFS3_SALT;
144             }
145         } else {
146             /* if there is a final string, use it as the string to
147                salt with, this is mostly useful with null salt for
148                v4 compat, and a cell name for afs compat */
149             salt->saltvalue.data = strdup(buf[i]);
150             if (salt->saltvalue.data == NULL) {
151                 krb5_set_error_string(context, "out of memory");
152                 return ENOMEM;
153             }
154             salt->saltvalue.length = strlen(buf[i]);
155         }
156     }
157     
158     if(enctypes == NULL || salt->salttype == 0) {
159         krb5_set_error_string(context, "bad value for default_keys `%s'", key);
160         return EINVAL;
161     }
162     
163     /* if no salt was specified make up default salt */
164     if(salt->saltvalue.data == NULL) {
165         if(salt->salttype == KRB5_PW_SALT)
166             ret = krb5_get_pw_salt(context, principal, salt);
167         else if(salt->salttype == KRB5_AFS3_SALT) {
168             krb5_realm *realm = krb5_princ_realm(context, principal);
169             salt->saltvalue.data = strdup(*realm);
170             if(salt->saltvalue.data == NULL) {
171                 krb5_set_error_string(context, "out of memory while "
172                                       "parsing salt specifiers");
173                 return ENOMEM;
174             }
175             strlwr(salt->saltvalue.data);
176             salt->saltvalue.length = strlen(*realm);
177         }
178     }
179
180     *ret_enctypes = malloc(sizeof(enctypes[0]) * num_enctypes);
181     if (*ret_enctypes == NULL) {
182         krb5_free_salt(context, *salt);
183         krb5_set_error_string(context, "out of memory");
184         return ENOMEM;
185     }
186     memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * num_enctypes);
187     *ret_num_enctypes = num_enctypes;
188
189     return 0;
190 }
191
192 static krb5_error_code
193 add_enctype_to_key_set(Key **key_set, size_t *nkeyset, 
194                        krb5_enctype enctype, krb5_salt *salt)
195 {
196     krb5_error_code ret;
197     Key key, *tmp;
198
199     memset(&key, 0, sizeof(key));
200
201     tmp = realloc(*key_set, (*nkeyset + 1) * sizeof((*key_set)[0]));
202     if (tmp == NULL)
203         return ENOMEM;
204     
205     *key_set = tmp;
206
207     key.key.keytype = enctype;
208     key.key.keyvalue.length = 0;
209     key.key.keyvalue.data = NULL;
210     
211     if (salt) {
212         key.salt = malloc(sizeof(*key.salt));
213         if (key.salt == NULL) {
214             free_Key(&key);
215             return ENOMEM;
216         }
217         
218         key.salt->type = salt->salttype;
219         krb5_data_zero (&key.salt->salt);
220         
221         ret = krb5_data_copy(&key.salt->salt, 
222                              salt->saltvalue.data, 
223                              salt->saltvalue.length);
224         if (ret) {
225             free_Key(&key);
226             return ret;
227         }
228     } else
229         key.salt = NULL;
230     
231     (*key_set)[*nkeyset] = key;
232     
233     *nkeyset += 1;
234
235     return 0;
236 }
237
238
239 /*
240  * Generate the `key_set' from the [kadmin]default_keys statement. If
241  * `no_salt' is set, salt is not important (and will not be set) since
242  * its random keys that is going to be created.
243  */
244
245 krb5_error_code
246 hdb_generate_key_set(krb5_context context, krb5_principal principal,
247                      Key **ret_key_set, size_t *nkeyset, int no_salt)
248 {
249     char **ktypes, **kp;
250     krb5_error_code ret;
251     Key *k, *key_set;
252     int i, j;
253     char *default_keytypes[] = {
254         "des:pw-salt",
255         "aes256-cts-hmac-sha1-96:pw-salt",
256         "des3-cbc-sha1:pw-salt",
257         "arcfour-hmac-md5:pw-salt",
258         NULL
259     };
260     
261     ktypes = krb5_config_get_strings(context, NULL, "kadmin",
262                                      "default_keys", NULL);
263     if (ktypes == NULL)
264         ktypes = default_keytypes;
265
266     if (ktypes == NULL)
267         abort();
268
269     *ret_key_set = key_set = NULL;
270     *nkeyset = 0;
271
272     ret = 0;
273  
274     for(kp = ktypes; kp && *kp; kp++) {
275         const char *p;
276         krb5_salt salt;
277         krb5_enctype *enctypes;
278         size_t num_enctypes;
279
280         p = *kp;
281         /* check alias */
282         if(strcmp(p, "v5") == 0)
283             p = "pw-salt";
284         else if(strcmp(p, "v4") == 0)
285             p = "des:pw-salt:";
286         else if(strcmp(p, "afs") == 0 || strcmp(p, "afs3") == 0)
287             p = "des:afs3-salt";
288         else if (strcmp(p, "arcfour-hmac-md5") == 0)
289             p = "arcfour-hmac-md5:pw-salt";
290             
291         memset(&salt, 0, sizeof(salt));
292
293         ret = parse_key_set(context, p,
294                             &enctypes, &num_enctypes, &salt, principal);
295         if (ret) {
296             krb5_warn(context, ret, "bad value for default_keys `%s'", *kp);
297             ret = 0;
298             continue;
299         }
300
301         for (i = 0; i < num_enctypes; i++) {
302             /* find duplicates */
303             for (j = 0; j < *nkeyset; j++) {
304
305                 k = &key_set[j];
306
307                 if (k->key.keytype == enctypes[i]) {
308                     if (no_salt)
309                         break;
310                     if (k->salt == NULL && salt.salttype == KRB5_PW_SALT)
311                         break;
312                     if (k->salt->type == salt.salttype &&
313                         k->salt->salt.length == salt.saltvalue.length &&
314                         memcmp(k->salt->salt.data, salt.saltvalue.data, 
315                                salt.saltvalue.length) == 0)
316                         break;
317                 }
318             }
319             /* not a duplicate, lets add it */
320             if (j == *nkeyset) {
321                 ret = add_enctype_to_key_set(&key_set, nkeyset, enctypes[i], 
322                                              no_salt ? NULL : &salt);
323                 if (ret) {
324                     free(enctypes);
325                     krb5_free_salt(context, salt);
326                     goto out;
327                 }
328             }
329         }
330         free(enctypes);
331         krb5_free_salt(context, salt);
332     }
333     
334     *ret_key_set = key_set;
335
336  out:
337     if (ktypes != default_keytypes)
338         krb5_config_free_strings(ktypes);
339
340     if (ret) {
341         krb5_warn(context, ret, 
342                   "failed to parse the [kadmin]default_keys values");
343
344         for (i = 0; i < *nkeyset; i++)
345             free_Key(&key_set[i]);
346         free(key_set);
347     } else if (*nkeyset == 0) {
348         krb5_warnx(context, 
349                    "failed to parse any of the [kadmin]default_keys values");
350         ret = EINVAL; /* XXX */
351     }
352
353     return ret;
354 }
355
356
357 krb5_error_code
358 hdb_generate_key_set_password(krb5_context context, 
359                               krb5_principal principal, 
360                               const char *password, 
361                               Key **keys, size_t *num_keys) 
362 {
363     krb5_error_code ret;
364     int i;
365
366     ret = hdb_generate_key_set(context, principal,
367                                 keys, num_keys, 0);
368     if (ret)
369         return ret;
370
371     for (i = 0; i < (*num_keys); i++) {
372         krb5_salt salt;
373
374         salt.salttype = (*keys)[i].salt->type;
375         salt.saltvalue.length = (*keys)[i].salt->salt.length;
376         salt.saltvalue.data = (*keys)[i].salt->salt.data;
377
378         ret = krb5_string_to_key_salt (context,
379                                        (*keys)[i].key.keytype,
380                                        password,
381                                        salt,
382                                        &(*keys)[i].key);
383
384         if(ret)
385             break;
386     }
387
388     if(ret) {
389         hdb_free_keys (context, *num_keys, *keys);
390         return ret;
391     }
392     return ret;
393 }