root/source4/heimdal/lib/hdb/keys.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. hdb_free_keys
  2. parse_key_set
  3. add_enctype_to_key_set
  4. hdb_generate_key_set
  5. hdb_generate_key_set_password

   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$");
  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)
     /* [<][>][^][v][top][bottom][index][help] */
  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 static const krb5_enctype des_etypes[] = {
  72     ETYPE_DES_CBC_MD5,
  73     ETYPE_DES_CBC_MD4,
  74     ETYPE_DES_CBC_CRC
  75 };
  76 
  77 static const krb5_enctype all_etypes[] = {
  78     ETYPE_AES256_CTS_HMAC_SHA1_96,
  79     ETYPE_ARCFOUR_HMAC_MD5,
  80     ETYPE_DES3_CBC_SHA1
  81 };
  82 
  83 static krb5_error_code
  84 parse_key_set(krb5_context context, const char *key,
     /* [<][>][^][v][top][bottom][index][help] */
  85               krb5_enctype **ret_enctypes, size_t *ret_num_enctypes,
  86               krb5_salt *salt, krb5_principal principal)
  87 {
  88     const char *p;
  89     char buf[3][256];
  90     int num_buf = 0;
  91     int i, num_enctypes = 0;
  92     krb5_enctype e;
  93     const krb5_enctype *enctypes = NULL;
  94     krb5_error_code ret;
  95 
  96     p = key;
  97 
  98     *ret_enctypes = NULL;
  99     *ret_num_enctypes = 0;
 100 
 101     /* split p in a list of :-separated strings */
 102     for(num_buf = 0; num_buf < 3; num_buf++)
 103         if(strsep_copy(&p, ":", buf[num_buf], sizeof(buf[num_buf])) == -1)
 104             break;
 105 
 106     salt->saltvalue.data = NULL;
 107     salt->saltvalue.length = 0;
 108 
 109     for(i = 0; i < num_buf; i++) {
 110         if(enctypes == NULL && num_buf > 1) {
 111             /* this might be a etype specifier */
 112             /* XXX there should be a string_to_etypes handling
 113                special cases like `des' and `all' */
 114             if(strcmp(buf[i], "des") == 0) {
 115                 enctypes = des_etypes;
 116                 num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]);
 117             } else if(strcmp(buf[i], "des3") == 0) {
 118                 e = ETYPE_DES3_CBC_SHA1;
 119                 enctypes = &e;
 120                 num_enctypes = 1;
 121             } else {
 122                 ret = krb5_string_to_enctype(context, buf[i], &e);
 123                 if (ret == 0) {
 124                     enctypes = &e;
 125                     num_enctypes = 1;
 126                 } else
 127                     return ret;
 128             }
 129             continue;
 130         }
 131         if(salt->salttype == 0) {
 132             /* interpret string as a salt specifier, if no etype
 133                is set, this sets default values */
 134             /* XXX should perhaps use string_to_salttype, but that
 135                interface sucks */
 136             if(strcmp(buf[i], "pw-salt") == 0) {
 137                 if(enctypes == NULL) {
 138                     enctypes = all_etypes;
 139                     num_enctypes = sizeof(all_etypes)/sizeof(all_etypes[0]);
 140                 }
 141                 salt->salttype = KRB5_PW_SALT;
 142             } else if(strcmp(buf[i], "afs3-salt") == 0) {
 143                 if(enctypes == NULL) {
 144                     enctypes = des_etypes;
 145                     num_enctypes = sizeof(des_etypes)/sizeof(des_etypes[0]);
 146                 }
 147                 salt->salttype = KRB5_AFS3_SALT;
 148             }
 149             continue;
 150         }
 151 
 152         {
 153             /* if there is a final string, use it as the string to
 154                salt with, this is mostly useful with null salt for
 155                v4 compat, and a cell name for afs compat */
 156             salt->saltvalue.data = strdup(buf[i]);
 157             if (salt->saltvalue.data == NULL) {
 158                 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
 159                 return ENOMEM;
 160             }
 161             salt->saltvalue.length = strlen(buf[i]);
 162         }
 163     }
 164 
 165     if(enctypes == NULL || salt->salttype == 0) {
 166         krb5_set_error_message(context, EINVAL, "bad value for default_keys `%s'", key);
 167         return EINVAL;
 168     }
 169 
 170     /* if no salt was specified make up default salt */
 171     if(salt->saltvalue.data == NULL) {
 172         if(salt->salttype == KRB5_PW_SALT)
 173             ret = krb5_get_pw_salt(context, principal, salt);
 174         else if(salt->salttype == KRB5_AFS3_SALT) {
 175             krb5_realm *realm = krb5_princ_realm(context, principal);
 176             salt->saltvalue.data = strdup(*realm);
 177             if(salt->saltvalue.data == NULL) {
 178                 krb5_set_error_message(context, ENOMEM,
 179                                        "out of memory while "
 180                                        "parsing salt specifiers");
 181                 return ENOMEM;
 182             }
 183             strlwr(salt->saltvalue.data);
 184             salt->saltvalue.length = strlen(*realm);
 185         }
 186     }
 187 
 188     *ret_enctypes = malloc(sizeof(enctypes[0]) * num_enctypes);
 189     if (*ret_enctypes == NULL) {
 190         krb5_free_salt(context, *salt);
 191         krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
 192         return ENOMEM;
 193     }
 194     memcpy(*ret_enctypes, enctypes, sizeof(enctypes[0]) * num_enctypes);
 195     *ret_num_enctypes = num_enctypes;
 196 
 197     return 0;
 198 }
 199 
 200 static krb5_error_code
 201 add_enctype_to_key_set(Key **key_set, size_t *nkeyset,
     /* [<][>][^][v][top][bottom][index][help] */
 202                        krb5_enctype enctype, krb5_salt *salt)
 203 {
 204     krb5_error_code ret;
 205     Key key, *tmp;
 206 
 207     memset(&key, 0, sizeof(key));
 208 
 209     tmp = realloc(*key_set, (*nkeyset + 1) * sizeof((*key_set)[0]));
 210     if (tmp == NULL)
 211         return ENOMEM;
 212 
 213     *key_set = tmp;
 214 
 215     key.key.keytype = enctype;
 216     key.key.keyvalue.length = 0;
 217     key.key.keyvalue.data = NULL;
 218 
 219     if (salt) {
 220         key.salt = malloc(sizeof(*key.salt));
 221         if (key.salt == NULL) {
 222             free_Key(&key);
 223             return ENOMEM;
 224         }
 225         
 226         key.salt->type = salt->salttype;
 227         krb5_data_zero (&key.salt->salt);
 228         
 229         ret = krb5_data_copy(&key.salt->salt,
 230                              salt->saltvalue.data,
 231                              salt->saltvalue.length);
 232         if (ret) {
 233             free_Key(&key);
 234             return ret;
 235         }
 236     } else
 237         key.salt = NULL;
 238 
 239     (*key_set)[*nkeyset] = key;
 240 
 241     *nkeyset += 1;
 242 
 243     return 0;
 244 }
 245 
 246 
 247 /*
 248  * Generate the `key_set' from the [kadmin]default_keys statement. If
 249  * `no_salt' is set, salt is not important (and will not be set) since
 250  * it's random keys that is going to be created.
 251  */
 252 
 253 krb5_error_code
 254 hdb_generate_key_set(krb5_context context, krb5_principal principal,
     /* [<][>][^][v][top][bottom][index][help] */
 255                      Key **ret_key_set, size_t *nkeyset, int no_salt)
 256 {
 257     char **ktypes, **kp;
 258     krb5_error_code ret;
 259     Key *k, *key_set;
 260     int i, j;
 261     char *default_keytypes[] = {
 262         "des:pw-salt",
 263         "aes256-cts-hmac-sha1-96:pw-salt",
 264         "des3-cbc-sha1:pw-salt",
 265         "arcfour-hmac-md5:pw-salt",
 266         NULL
 267     };
 268 
 269     ktypes = krb5_config_get_strings(context, NULL, "kadmin",
 270                                      "default_keys", NULL);
 271     if (ktypes == NULL)
 272         ktypes = default_keytypes;
 273 
 274     if (ktypes == NULL)
 275         abort();
 276 
 277     *ret_key_set = key_set = NULL;
 278     *nkeyset = 0;
 279 
 280     ret = 0;
 281 
 282     for(kp = ktypes; kp && *kp; kp++) {
 283         const char *p;
 284         krb5_salt salt;
 285         krb5_enctype *enctypes;
 286         size_t num_enctypes;
 287 
 288         p = *kp;
 289         /* check alias */
 290         if(strcmp(p, "v5") == 0)
 291             p = "pw-salt";
 292         else if(strcmp(p, "v4") == 0)
 293             p = "des:pw-salt:";
 294         else if(strcmp(p, "afs") == 0 || strcmp(p, "afs3") == 0)
 295             p = "des:afs3-salt";
 296         else if (strcmp(p, "arcfour-hmac-md5") == 0)
 297             p = "arcfour-hmac-md5:pw-salt";
 298         
 299         memset(&salt, 0, sizeof(salt));
 300 
 301         ret = parse_key_set(context, p,
 302                             &enctypes, &num_enctypes, &salt, principal);
 303         if (ret) {
 304             krb5_warn(context, ret, "bad value for default_keys `%s'", *kp);
 305             ret = 0;
 306             continue;
 307         }
 308 
 309         for (i = 0; i < num_enctypes; i++) {
 310             /* find duplicates */
 311             for (j = 0; j < *nkeyset; j++) {
 312 
 313                 k = &key_set[j];
 314 
 315                 if (k->key.keytype == enctypes[i]) {
 316                     if (no_salt)
 317                         break;
 318                     if (k->salt == NULL && salt.salttype == KRB5_PW_SALT)
 319                         break;
 320                     if (k->salt->type == salt.salttype &&
 321                         k->salt->salt.length == salt.saltvalue.length &&
 322                         memcmp(k->salt->salt.data, salt.saltvalue.data,
 323                                salt.saltvalue.length) == 0)
 324                         break;
 325                 }
 326             }
 327             /* not a duplicate, lets add it */
 328             if (j == *nkeyset) {
 329                 ret = add_enctype_to_key_set(&key_set, nkeyset, enctypes[i],
 330                                              no_salt ? NULL : &salt);
 331                 if (ret) {
 332                     free(enctypes);
 333                     krb5_free_salt(context, salt);
 334                     goto out;
 335                 }
 336             }
 337         }
 338         free(enctypes);
 339         krb5_free_salt(context, salt);
 340     }
 341 
 342     *ret_key_set = key_set;
 343 
 344  out:
 345     if (ktypes != default_keytypes)
 346         krb5_config_free_strings(ktypes);
 347 
 348     if (ret) {
 349         krb5_warn(context, ret,
 350                   "failed to parse the [kadmin]default_keys values");
 351 
 352         for (i = 0; i < *nkeyset; i++)
 353             free_Key(&key_set[i]);
 354         free(key_set);
 355     } else if (*nkeyset == 0) {
 356         krb5_warnx(context,
 357                    "failed to parse any of the [kadmin]default_keys values");
 358         ret = EINVAL; /* XXX */
 359     }
 360 
 361     return ret;
 362 }
 363 
 364 
 365 krb5_error_code
 366 hdb_generate_key_set_password(krb5_context context,
     /* [<][>][^][v][top][bottom][index][help] */
 367                               krb5_principal principal,
 368                               const char *password,
 369                               Key **keys, size_t *num_keys)
 370 {
 371     krb5_error_code ret;
 372     int i;
 373 
 374     ret = hdb_generate_key_set(context, principal,
 375                                 keys, num_keys, 0);
 376     if (ret)
 377         return ret;
 378 
 379     for (i = 0; i < (*num_keys); i++) {
 380         krb5_salt salt;
 381 
 382         salt.salttype = (*keys)[i].salt->type;
 383         salt.saltvalue.length = (*keys)[i].salt->salt.length;
 384         salt.saltvalue.data = (*keys)[i].salt->salt.data;
 385 
 386         ret = krb5_string_to_key_salt (context,
 387                                        (*keys)[i].key.keytype,
 388                                        password,
 389                                        salt,
 390                                        &(*keys)[i].key);
 391 
 392         if(ret)
 393             break;
 394     }
 395 
 396     if(ret) {
 397         hdb_free_keys (context, *num_keys, *keys);
 398         return ret;
 399     }
 400     return ret;
 401 }

/* [<][>][^][v][top][bottom][index][help] */