Stop using SALT_TYPE_AFS_LENGTH
[krb5.git] / src / lib / crypto / krb / string_to_key.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  * Copyright (C) 1998 by the FundsXpress, INC.
4  *
5  * All rights reserved.
6  *
7  * Export of this software from the United States of America may require
8  * a specific license from the United States Government.  It is the
9  * responsibility of any person or organization contemplating export to
10  * obtain such a license before exporting.
11  *
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of FundsXpress. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  FundsXpress makes no representations about the suitability of
20  * this software for any purpose.  It is provided "as is" without express
21  * or implied warranty.
22  *
23  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
25  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26  */
27
28 #include "crypto_int.h"
29
30 krb5_error_code KRB5_CALLCONV
31 krb5_c_string_to_key(krb5_context context, krb5_enctype enctype,
32                      const krb5_data *string, const krb5_data *salt,
33                      krb5_keyblock *key)
34 {
35     return krb5_c_string_to_key_with_params(context, enctype, string, salt,
36                                             NULL, key);
37 }
38
39 krb5_error_code KRB5_CALLCONV
40 krb5_c_string_to_key_with_params(krb5_context context, krb5_enctype enctype,
41                                  const krb5_data *string,
42                                  const krb5_data *salt,
43                                  const krb5_data *params, krb5_keyblock *key)
44 {
45     krb5_error_code ret;
46     const struct krb5_keytypes *ktp;
47     size_t keylength;
48
49     ktp = find_enctype(enctype);
50     if (ktp == NULL)
51         return KRB5_BAD_ENCTYPE;
52     keylength = ktp->enc->keylength;
53
54     /* Fail gracefully if someone is using the old AFS string-to-key hack. */
55     if (salt != NULL && salt->length == SALT_TYPE_AFS_LENGTH)
56         return EINVAL;
57
58     key->contents = malloc(keylength);
59     if (key->contents == NULL)
60         return ENOMEM;
61
62     key->magic = KV5M_KEYBLOCK;
63     key->enctype = enctype;
64     key->length = keylength;
65
66     ret = (*ktp->str2key)(ktp, string, salt, params, key);
67     if (ret) {
68         zapfree(key->contents, keylength);
69         key->length = 0;
70         key->contents = NULL;
71     }
72
73     return ret;
74 }